.net Core API 添加 NLog

news/2024/12/4 18:13:33/

nlog.config

<?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-AspNetCore.txt">
​<!-- enable asp.net 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:\temp\nlog-AspNetCore-all-${shortdate}.log"layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${level:uppercase=true}|${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:\temp\nlog-AspNetCore-own-${shortdate}.log"layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}" />
​<!--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>
<?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"><!-- 启用.net core的核心布局渲染器 --><extensions><add assembly="NLog.Web.AspNetCore" /></extensions><!-- 写入日志的目标配置 --><targets><!-- 调试  --><target xsi:type="File" name="debug" fileName="logs/debug-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /><!-- 警告  --><target xsi:type="File" name="warn" fileName="logs/warn-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /><!-- 错误  --><target xsi:type="File" name="error" fileName="logs/error-${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><!-- 调试  --><logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="debug" /><!--跳过不重要的微软日志--><logger name="Microsoft.*" maxlevel="Info" final="true" /><!-- 警告  --><logger name="*" minlevel="Info" maxlevel="Warn" writeTo="warn" /><!-- 错误  --><logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="error" /></rules>
</nlog>

program.cs 

public class Program{public static void Main(string[] args){var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
​try{logger.Debug("init main");CreateHostBuilder(args).Build().Run();}catch (Exception exception){//NLog: catch setup errorslogger.Error(exception, "Stopped program because of exception");throw;}finally{// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)NLog.LogManager.Shutdown();}}
​public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();}).ConfigureLogging(logging =>{logging.ClearProviders();logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);}).UseNLog();  // NLog: Setup NLog for Dependency injection}

NuGet packages:NLog、NLog.Web.AspNetCore 


http://www.ppmy.cn/news/784468.html

相关文章

Spyder输入中文后光标消失

在spyder中输入中文后&#xff0c;存在光标消失问题&#xff0c;可使用如下方式修正&#xff1a; 中文输入法状态&#xff0c;点击键盘任意一个字母&#xff0c;再点击backspace键&#xff0c;光标就会重新出现.

ubuntu光标消失

解决方案&#xff1a;echo -e “\033[?25h”

ue4当点击UI界面时,鼠标会消失不见

点击上图中的&#xff0c;打开关卡蓝图&#xff0c;在编辑器中&#xff0c;新增“Get Player Controller”,调用SetMouseCursor,勾选checkBox,

光标消失了要怎么调回来?

在打字的时候&#xff0c;我们偶尔会碰到光标从一条闪烁的竖线突然变成了下划线&#xff0c;或者直接整个消失的情况&#xff0c;那么遇见这种情况要怎么把光标变回原样呢&#xff1f; 苹果电脑按住Fn再按Enter(回车)键 win电脑按右上角Insert键 即可恢复&#xff0c;试一下呐…

解决Ubuntu 18.04 系统桌面鼠标光标消失的问题

最近安装Ubuntu 18.04的时候&#xff0c;遇到一个问题&#xff0c;具体表现是鼠标光标不见&#xff0c;并且无法恢复&#xff0c;我试着重新解压ISO文件也没有解决&#xff0c;而这个问题我找了挺多结果的&#xff0c;有重新安装驱动的&#xff0c;有说重新安装桌面的&#xff…

idea页面不显示鼠标光标了?_日记: router-view 页面不显示

今天学习Element 时候使用router-view 页面会不显示,而且也没有报错,后来发现2个问题 1.routes 的问题 import 这里的 routes 不是routers 也不是router . 2. 根元素问题 idapp <div idapp style"margin-top:10px"><router-view></router-view> &…

virtualbox鼠标不见了

解决方法 右边CTRL键为Virtualbox的控制键