Nlog相关资料:
1 nuget安装NLog.Web.AspNetCore
2 在web应用程序Program.cs中对照修改代码
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
1
2
3
4
5
6
7
8
9
publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging(logging=>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
3 在相应的控制器中修改
private readonly ILogger _logger;
public HomeController(ILogger logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("hello--index");
return View();
}
1
2
3
4
5
6
7
8
9
10
11
12
privatereadonlyILogger_logger;
publicHomeController(ILoggerlogger)
{
_logger=logger;
}
publicIActionResultIndex()
{
_logger.LogInformation("hello--index");
returnView();
}
4 在web应用程序中添加nlog.config配置文件,并设置为较新则复制或始终复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"/>
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"/>
Log4Net相关资料:
1 nuget安装Microsoft.Extensions.Logging.Log4Net.AspNetCore
2 修改web应用程序program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging((hostingContext, logging) =>
{
// The ILoggingBuilder minimum level determines the
// the lowest possible level for logging. The log4net
// level then sets the level that we actually log at.
logging.AddLog4Net();
logging.SetMinimumLevel(LogLevel.Debug);
});
1
2
3
4
5
6
7
8
9
10
11
publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging((hostingContext,logging)=>
{
// The ILoggingBuilder minimum level determines the
// the lowest possible level for logging. The log4net
// level then sets the level that we actually log at.
logging.AddLog4Net();
logging.SetMinimumLevel(LogLevel.Debug);
});
3 在相应的控制器中修改
private readonly ILogger _logger;
public HomeController(ILogger logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("hello--index");
return View();
}
1
2
3
4
5
6
7
8
9
10
11
12
privatereadonlyILogger_logger;
publicHomeController(ILoggerlogger)
{
_logger=logger;
}
publicIActionResultIndex()
{
_logger.LogInformation("hello--index");
returnView();
}
4 在Web应用程序中添加log4net.config,并设置为较新复制或始终复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行程序,会在Web应用程序目录下生成日志文件