【发布时间】:2020-05-26 20:57:31
【问题描述】:
我正在尝试配置 NLog 以将我自己的异常和系统异常记录到 2 个不同的日志文件中。
假设当我运行_logger.LogError("This is my exception.") 时,它应该被记录到myerrors.log,但是当服务器崩溃时,错误应该被记录到另一个文件中。
我现在遇到的问题是,系统错误都记录到了这两个文件中。
这是我的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=".\SystemLogs\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=".\SystemLogs\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=".\ErrorLogs\${shortdate}\errors-${shortdate}.log"
layout="${longdate}|${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="Error" writeTo="ownFile-web" />
</rules>
</nlog>
这是 appsettings.json 配置:
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Error",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
【问题讨论】:
-
当服务器崩溃时,您会在日志中看到什么?错误还是致命?在您的 nlog 配置规则中,您指定最低日志级别,这就是它同时登录的原因
-
这是正确的配置吗?我没有看到
myerrors.log -
@Julian
myerrors.log是一个占位符,在配置中它的errors-${shortdate}.log -
也许打印 ${logger} 表示“但是当服务器崩溃时”。我不知道那是什么。
标签: c# asp.net-core nlog