丁立武优秀作者
原创内容 来源:小居数码网 时间:2024-08-13 14:08:02 阅读() 收藏:33 分享:57 爆
导读:您正在阅读的是关于【数码知识】的问题,本文由科普作家协会,生活小能手,著名生活达人等整理监督编写。本文有1836个文字,大小约为6KB,预计阅读时间5分钟。
在ASP.NET Core 6.0中,官方提供了内置日志记录提供程序,如:
除了内置日志功能外,也有很多第三方日志记录提供程序,这里列举几个比较热门的日志组件:
接下来,我们便学习如何在程序里使用这些组件
新建一个名为log4net.config的配置文件
加入配置
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
<?xml version="1.0" encoding="utf-8" ?><log4net> <appender name="Console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </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> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </layout> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="WARN" /> <foreColor value="Yellow" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </layout> </appender> <root> <level value="TRACE" /> <appender-ref ref="RollingFile" /> <appender-ref ref="TraceAppender" /> <appender-ref ref="ConsoleAppender" /> </root></log4net>
12
builder.Logging.ClearProviders();builder.Logging.AddLog4Net();
然后在需要记录日志的地方引入,使用ILogger,这里我依然以OrderController为例
1234567
private readonly ILogger<OrdersController> _logger; public OrdersController(DemoContext context, ILogger<OrdersController> logger) { _context = context; _logger = logger; }
当我们访问该接口后,会在控制台打印出相关信息
亦会在运行目录下生成名为example.log的日志文件
新建一个名为nlog.config的配置文件,并加入如下配置
123456789101112131415161718192021222324252627282930313233343536373839404142
<?xml version="1.0" encoding="utf-8" ?><nlog xmlns="http:// core layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <!-- File Target for all log messages with basic details --> <target xsi:type="File" name="allfile" fileName="c:tempnlog-AspNetCore6-all-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <!-- File Target for own log messages with extra web details using some ASP.NET core renderers --> <target xsi:type="File" name="ownFile-web" fileName="c:tempnlog-AspNetCore6-own-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}| body: ${aspnet-request-posted-body}" /> <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection --> <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Output hosting lifetime messages to console target for faster startup detection --> <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" /> <!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) --> <logger name="Microsoft.*" maxlevel="Info" final="true" /> <logger name="System.Net.Http.*" maxlevel="Info" final="true" /> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules></nlog>
在Program.cs加入如下代码,
123
builder.Logging.ClearProviders();builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //日志级别builder.Host.UseNLog();
日志是使用方式不变,因为是直接替换的日志的组件,基于接口的日志调用方式不变。
这里为了演示,我就偷个懒,直接将信息写在代码中,先是配置日志规则
1234
Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception}") .CreateLogger();
然后在使用Serilog
1
builder.Host.UseSerilog(Log.Logger, dispose: true);
一般工作中,用的比较多的可能就是这三种了,听说,Serilog的性能比较好,我没有对比过,所以也不敢乱说,大家可以自己去了解一下。日记记录,就先了解到这里,接下来,我们继续去了解一下ASP.NET Core 6.0模型绑定
上面就是小居数码小编今天给大家介绍的关于(asp.netcorelog)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。
94%的朋友还想知道的:
(383)个朋友认为回复得到帮助。
部分文章信息来源于以及网友投稿,转载请说明出处。
本文标题:.netcore日志框架(asp.netcorelog):http://sjzlt.cn/shuma/155223.html