发布:2023/12/7 15:29:39作者:大数据 来源:大数据 浏览次数:556
Nlog相关资料:
https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-2
https://github.com/NLog/NLog.Web/tree/master/examples/ASP.NET%20Core%202/Visual%20Studio%202017/ASP.NET%20Core%202%20-%20VS2017
1 nuget安装NLog.Web.AspNetCore
2 在web应用程序Program.cs中对照修改代码
1 2 3 4 5 6 7 8 9 |
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureLogging(logging => { logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Trace); }) .UseNLog(); |
3 在相应的控制器中修改
1 2 3 4 5 6 7 8 9 10 11 12 |
private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("hello--index"); return View(); } |
4 在web应用程序中添加nlog.config配置文件,并设置为较新则复制或始终复制
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 |
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Info" internalLogFile="c:\temp\internal-nlog.txt"> <!-- enable asp.net core layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-{shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <!-- another file log, only own logs. Uses some ASP.NET core renderers --> <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo --> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog> |
Log4Net相关资料:
https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
1 nuget安装Microsoft.Extensions.Logging.Log4Net.AspNetCore
2 修改web应用程序program.cs
1 2 3 4 5 6 7 8 9 10 11 |
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .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 在相应的控制器中修改
1 2 3 4 5 6 7 8 9 10 11 12 |
private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("hello--index"); return View(); } |
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 |
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="DebugAppender" type="log4net.Appender.DebugAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="example.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="DebugAppender" /> <appender-ref ref="RollingFile" /> </root> </log4net> |
运行程序,会在Web应用程序目录下生成日志文件
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4