用NuGet管理器下载 Log4net 插件。
在更目录创建一个文件夹起名ConfigFile,里面新建一个Log4net.config 文档。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="true">
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<!--日志文件名开头-->
<file value="C:\\Logs\\Error\\WeiXin"/>
<!--是否追加到文件-->
<appendToFile value="true"/>
<!--混合使用日期和文件大小变换日志文件名-->
<rollingStyle value="Composite"/>
<!--最大变换数量-->
<maxSizeRollBackups value="10"/>
<!--最大文件大小-->
<maximumFileSize value="500KB"/>
<!--日期的格式-->
<staticLogFileName value="false"/>
<datePattern value="yyyyMMdd" log=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger : %message%newline"/>
</layout>
</appender>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<!--日志文件名开头-->
<file value="C:\\Logs\\Info\\WeiXin"/>
<!--是否追加到文件-->
<appendToFile value="true"/>
<!--混合使用日期和文件大小变换日志文件名-->
<rollingStyle value="Composite"/>
<!--最大变换数量-->
<maxSizeRollBackups value="10"/>
<!--最大文件大小-->
<maximumFileSize value="500KB"/>
<!--日期的格式-->
<staticLogFileName value="false"/>
<datePattern value="yyyyMMdd" log=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger : %message%newline"/>
</layout>
</appender>
<root>
<level value="All"/>
<appender-ref ref="ErrorAppender"/>
<appender-ref ref="InfoAppender"/>
</root>
</log4net>
</configuration>
在项目的 Global.asax 中添加全局启动项:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//下边这两行是
var log4netConfigPath = Server.MapPath("ConfigFile/log4net.config");
XmlConfigurator.Configure(new FileInfo(log4netConfigPath));
}
最后在控制器中的头部写上:
private static readonly ILog Log = LogManager.GetLogger(typeof(HomeController));
在代码块打印数据,输出到C盘的logs的文件夹中。
public JsonResult GetShowStudyPlan(DateTime data, int RYid)
{
string formattedDate = data.ToString("yyyy-MM-dd");
string Zhou = PublicClass.GetChineseDayOfWeek(data.DayOfWeek);
Log.Info("走到了这里");
ViewHome viewHome = new ViewHome();
string SQLXueXiQingBao = "select XueXiQingBaoID from (select FORMAT(StartDateTime, 'yyyy-MM-dd') StartDate,XueXiQingBaoID from [QTLSystem].[dbo].[XueXiQingBao] where UserID='" + RYid + "') tb where StartDate='" + data + "';";
string SQLLearningLog = "SELECT CAST(FLOOR(num / 60) AS VARCHAR) + '小时' + CAST(num % 60 AS VARCHAR) + '分钟' AS FormattedTime FROM (SELECT SUM(isnull(ShiChang,0)) AS num FROM [dbo].[LearningLog] WHERE UserID ='" + RYid + "' AND CONVERT(date, StartDateTime) = '2024-03-05') AS Subquery;";
string SQLStudyPlan = "SELECT RepeatDate,SubjectName,Minutes,StudyHours,* FROM [QTLSystem].[dbo].[StudyPlan] where UserID='" + RYid + "' order by Sequence;";
DataTable XueXidt = DataClass.GetDataSetBySql(SQLXueXiQingBao).Tables[0];
DataTable StudyPlan = DataClass.GetDataSetBySql(SQLStudyPlan).Tables[0];
DataSet dataSet = DataClass.GetDataSetBySql(SQLLearningLog);
//学请报告编号
}