英文:
Why do I get a log file for every logger call in NLog?
问题
你的问题是关于使用NLog在.NET 7中记录日志的配置问题。以下是你的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"
internalLogFile="c:\temp\internal.log"
internalLogLevel="Debug" >
<variable name="appName"
value="${gdc:item=assemblyName}" />
<variable name="appVersion"
value="${gdc:item=version}" />
<variable name="driveName"
value="C:" />
<variable name="rootFolder"
value="Users/Public/Documents/RSA" />
<variable name="pathName"
value="${driveName}/${rootFolder}/${appName}/Logs/${shortDate}" />
<variable name="HeaderLayout"
value="********* ${processname} Assembly: ${appName} Version: ${appVersion} Started @ ${longdate} *********" />
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target xsi:type="File"
name="logfile"
header="${HeaderLayout}"
fileName="C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss}.log"
layout="${processinfo:property=StartTime:format=HHmmss.ffff:cached=true} ${uppercase:${level}} ${callsite}:${callsite-linenumber} - ${message}${onexception: EXCEPTION OCCURRED:${exception:format=ToString,Data:maxInnerExceptionLevel=10:innerExceptionSeparator=String:separator=String:exceptionDataSeparator=string}}" />
</targets>
<rules>
<logger name="ProcessCeptorStreamingOutput" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>
以下是库的构造函数和日志记录器属性:
public static Logger logger { get; private set; }
/// <summary>
/// Initializes static members of the <see cref="T:CommonLogging.CLog" /> class.
/// </summary>
static CLog()
{
GlobalDiagnosticsContext.Set("assemblyName", GetAssemblyName());
GlobalDiagnosticsContext.Set("version", GetCurrentBuild());
logger = LogManager.GetLogger(GlobalDiagnosticsContext.Get("assemblyName"));
}
你的问题似乎是NLog在.NET 7中记录日志时出现了问题,它试图为每次记录日志都创建一个新的日志文件,而不是像在.NET 4.7.2中那样只创建一个日志文件。你可以检查NLog配置文件以确保它没有被意外地更改,还可以查看NLog的文档以获取关于在.NET 7中正确配置它的更多信息。希望这能帮助你解决问题。
英文:
I have a console application in .NET 7 and I am trying to get NLog to write a single log file for the console application. It wants to write a log file for every call to the logger. I use a small DLL with a class, static constructor and a static copy of a logger. Every project in the solution loads this DLL so all should be using the same logger. This all worked fine in .net 4.7.2 but is not in in .net 7.
Here is my updated nlog.config file based on Rolf's suggestions and the error I was receiving:
<?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"
internalLogFile="c:\temp\internal.log"
internalLogLevel="Debug" >
<variable name="appName"
value="${gdc:item=assemblyName}" />
<variable name="appVersion"
value="${gdc:item=version}" />
<variable name="driveName"
value="C:" />
<variable name="rootFolder"
value="Users/Public/Documents/RSA" />
<variable name="pathName"
value="${driveName}/${rootFolder}/${appName}/Logs/${shortDate}" />
<variable name="HeaderLayout"
value="********* ${processname} Assembly: ${appName} Version: ${appVersion} Started @ ${longdate} *********" />
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target xsi:type="File"
name="logfile"
header="${HeaderLayout}"
fileName="C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss}.log"
layout="${processinfo:property=StartTime:format=HHmmss.ffff:cached=true} ${uppercase:${level}} ${callsite}:${callsite-linenumber} - ${message}${onexception: EXCEPTION OCCURRED\:${exception:format=ToString,Data:maxInnerExceptionLevel=10:innerExceptionSeparator=String:separator=String:exceptionDataSeparator=string}}" />
</targets>
<rules>
<logger name="ProcessCeptorStreamingOutput" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>
And the constructor for the library and the logger property:
public static Logger logger { get; private set; }
/// <summary>
/// Initializes static members of the <see cref="T:CommonLogging.CLog" /> class.
/// </summary>
static CLog()
{
GlobalDiagnosticsContext.Set("assemblyName", GetAssemblyName());
GlobalDiagnosticsContext.Set("version", GetCurrentBuild());
logger = LogManager.GetLogger(GlobalDiagnosticsContext.Get("assemblyName"));
}
In the internal log you can see where it opens and writes to two files (I limited the number of calls to the logger):
2023-05-25 08:49:31.2218 Info Message Template Auto Format enabled
2023-05-25 08:49:31.2399 Debug ScanAssembly('NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2023-05-25 08:49:31.3033 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'assemblyName'
2023-05-25 08:49:31.3033 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'version'
2023-05-25 08:49:31.3033 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'assemblyName'
2023-05-25 08:49:31.3472 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'assemblyName'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'version'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.Targets.FileTarget.Name' to 'logfile'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.Targets.FileTarget.Header' to '********* ${processname} Assembly: ${gdc:item=assemblyName} Version: ${gdc:item=version} Started @ ${longdate} *********'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'assemblyName'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'version'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.Targets.FileTarget.FileName' to 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${gdc:item=assemblyName}${date:format=HHmmss}.log'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.LayoutRenderers.GdcLayoutRenderer.Item' to 'assemblyName'
2023-05-25 08:49:31.3522 Debug Setting 'NLog.LayoutRenderers.DateLayoutRenderer.Format' to 'HHmmss'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.Targets.FileTarget.Layout' to '${processinfo:property=StartTime:format=HHmmss.ffff:cached=true} ${uppercase:${level}} ${callsite}:${callsite-linenumber} - ${message}${onexception: EXCEPTION OCCURRED\:${exception:format=ToString,Data:maxInnerExceptionLevel=10:innerExceptionSeparator=String:separator=String:exceptionDataSeparator=string}}'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ProcessInfoLayoutRenderer.Property' to 'StartTime'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ProcessInfoLayoutRenderer.Format' to 'HHmmss.ffff'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper.Cached' to 'true'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper.Inner' to '${level}'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.Wrappers.OnExceptionLayoutRendererWrapper.Inner' to ' EXCEPTION OCCURRED:${exception:format=ToString,Data:maxInnerExceptionLevel=10:innerExceptionSeparator=String:separator=String:exceptionDataSeparator=string}'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ExceptionLayoutRenderer.Format' to 'ToString,Data'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ExceptionLayoutRenderer.MaxInnerExceptionLevel' to '10'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ExceptionLayoutRenderer.InnerExceptionSeparator' to 'String'
2023-05-25 08:49:31.3671 Debug Setting 'NLog.LayoutRenderers.ExceptionLayoutRenderer.Separator' to 'String'
2023-05-25 08:49:31.3820 Debug Setting 'NLog.LayoutRenderers.ExceptionLayoutRenderer.ExceptionDataSeparator' to 'string'
2023-05-25 08:49:31.3820 Debug Adding target NLog.Targets.FileTarget(Name=logfile)
2023-05-25 08:49:31.3820 Info Registered target NLog.Targets.FileTarget(Name=logfile)
2023-05-25 08:49:31.3820 Debug Watching file-filter 'NLog.config' in directory: C:\SoftwareDevel\Div8\CEPTOR\ProcessCeptorStreamingOutput\ProcessCeptorStreamingOutput\bin\x64\Debug\net7.0
2023-05-25 08:49:31.3820 Debug --- NLog configuration dump ---
2023-05-25 08:49:31.3820 Debug Targets:
2023-05-25 08:49:31.3820 Debug FileTarget(Name=logfile)
2023-05-25 08:49:31.3820 Debug Rules:
2023-05-25 08:49:31.3820 Debug logNamePattern: (ProcessCeptorStreamingOutput:Equals) levels: [ Trace Debug Info Warn Error Fatal ] writeTo: [ logfile ]
2023-05-25 08:49:31.3820 Debug --- End of NLog configuration dump ---
2023-05-25 08:49:31.4002 Info Validating config: TargetNames=logfile, ConfigItems=35, FilePath=C:\SoftwareDevel\Div8\CEPTOR\ProcessCeptorStreamingOutput\ProcessCeptorStreamingOutput\bin\x64\Debug\net7.0\NLog.config
2023-05-25 08:49:31.4002 Debug Unused target checking is started... Rule Count: 1, Target Count: 1
2023-05-25 08:49:31.4002 Debug Unused target checking is completed. Total Rule Count: 1, Total Target Count: 1, Unused Target Count: 0
2023-05-25 08:49:31.4492 Info Configuration initialized.
2023-05-25 08:49:31.4600 Info NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 5.1.4.1703. Product version: 5.1.4+589ee8b1398665c1810660c5434d0226b0ccf09f. GlobalAssemblyCache: False
2023-05-25 08:49:31.4600 Debug Targets configured when LogLevel >= Trace for Logger: ProcessCeptorStreamingOutput
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Trace] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Debug] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Info] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Warn] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Error] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Fatal] => logfile
2023-05-25 08:49:31.4600 Info Validating config: TargetNames=logfile, ConfigItems=35, FilePath=C:\SoftwareDevel\Div8\CEPTOR\ProcessCeptorStreamingOutput\ProcessCeptorStreamingOutput\bin\x64\Debug\net7.0\NLog.config
2023-05-25 08:49:31.4600 Debug Targets configured when LogLevel >= Trace for Logger: ProcessCeptorStreamingOutput
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Trace] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Debug] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Info] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Warn] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Error] => logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Fatal] => logfile
2023-05-25 08:49:31.5341 Debug FileTarget(Name=logfile): Preparing for new file: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log'
2023-05-25 08:49:31.5341 Debug FileTarget(Name=logfile): Creating file appender: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log'
2023-05-25 08:49:37.6631 Debug FileTarget(Name=logfile): Preparing for new file: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log'
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): Creating file appender: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log'
2023-05-25 08:49:37.6639 Info Shutdown() called. Logger closing...
2023-05-25 08:49:37.6639 Info Closing old configuration.
2023-05-25 08:49:37.6639 Debug LogFactory Flush with timeout=15 secs
2023-05-25 08:49:37.6639 Debug Flush completed
2023-05-25 08:49:37.6639 Debug Closing logging configuration...
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): Closing...
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): FileAppender Invalidate Closing File: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log'
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): FileAppender Invalidate Closing File: 'C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log'
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): Closed.
2023-05-25 08:49:37.6639 Debug Finished closing logging configuration.
2023-05-25 08:49:37.6790 Debug Stopping file watching for path 'C:\SoftwareDevel\Div8\CEPTOR\ProcessCeptorStreamingOutput\ProcessCeptorStreamingOutput\bin\x64\Debug\net7.0' filter 'NLog.config'
2023-05-25 08:49:37.6790 Debug Targets not configured for Logger: ProcessCeptorStreamingOutput
2023-05-25 08:49:37.6790 Info Logger has been closed down.
2023-05-25 08:49:38.8302 Info AppDomain Shutting down. LogFactory closing...
2023-05-25 08:49:38.8302 Info LogFactory has been closed.
Thanks
答案1
得分: 1
If you want single file, then consider changing ${date:format=HHmmss}
to ${date:format=HHmmss:cached=true}
for the File-Target FileName.
Like this:
<targets>
<target xsi:type="File" name="logfile"
fileName="C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss:cached=true}.log" />
</targets>
Alternative one could use ${processinfo:property=StartTime:format=HHmmss:cached=true}
See also: 链接
See also: 链接
英文:
If you want single file, then consider changing ${date:format=HHmmss}
to ${date:format=HHmmss:cached=true}
for the File-Target FileName.
Like this:
<targets>
<target xsi:type="File" name="logfile"
fileName="C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss:cached=true}.log" />
</targets>
Alternative one could use ${processinfo:property=StartTime:format=HHmmss:cached=true}
See also: https://github.com/NLog/NLog/wiki/ProcessInfo-Layout-Renderer
See also: https://github.com/nlog/NLog/wiki/Cached-Layout-Renderer
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论