为什么在NLog中每次记录器调用都会获得一个日志文件?

huangapple go评论45阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;

&lt;nlog xmlns=&quot;http://www.nlog-project.org/schemas/NLog.xsd&quot; 
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      autoReload=&quot;true&quot;
      internalLogFile=&quot;c:\temp\internal.log&quot;
      internalLogLevel=&quot;Debug&quot; &gt;

	&lt;variable name=&quot;appName&quot;
              value=&quot;${gdc:item=assemblyName}&quot; /&gt;
	&lt;variable name=&quot;appVersion&quot;
              value=&quot;${gdc:item=version}&quot; /&gt;
	&lt;variable name=&quot;driveName&quot;
              value=&quot;C:&quot; /&gt;
	&lt;variable name=&quot;rootFolder&quot;
              value=&quot;Users/Public/Documents/RSA&quot; /&gt;
	&lt;variable name=&quot;pathName&quot;
              value=&quot;${driveName}/${rootFolder}/${appName}/Logs/${shortDate}&quot; /&gt;
	&lt;variable name=&quot;HeaderLayout&quot;
              value=&quot;********* ${processname} Assembly: ${appName} Version: ${appVersion} Started @ ${longdate} *********&quot; /&gt;

	&lt;!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   --&gt;
	&lt;targets&gt;
		&lt;target xsi:type=&quot;File&quot;
				name=&quot;logfile&quot;
				header=&quot;${HeaderLayout}&quot;
				fileName=&quot;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss}.log&quot;
				layout=&quot;${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}}&quot; /&gt;

	&lt;/targets&gt;

	&lt;rules&gt;
		&lt;logger name=&quot;ProcessCeptorStreamingOutput&quot; minlevel=&quot;Trace&quot; writeTo=&quot;logfile&quot; /&gt;
	&lt;/rules&gt;
&lt;/nlog&gt;

And the constructor for the library and the logger property:

public static Logger logger { get; private set; }

/// &lt;summary&gt;
/// Initializes static members of the &lt;see cref=&quot;T:CommonLogging.CLog&quot; /&gt; class.
/// &lt;/summary&gt;
static CLog()
{
    GlobalDiagnosticsContext.Set(&quot;assemblyName&quot;, GetAssemblyName());
    GlobalDiagnosticsContext.Set(&quot;version&quot;, GetCurrentBuild());

    logger = LogManager.GetLogger(GlobalDiagnosticsContext.Get(&quot;assemblyName&quot;));

}

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(&#39;NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c&#39;)
2023-05-25 08:49:31.3033 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;assemblyName&#39;
2023-05-25 08:49:31.3033 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;version&#39;
2023-05-25 08:49:31.3033 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;assemblyName&#39;
2023-05-25 08:49:31.3472 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;assemblyName&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;version&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.Targets.FileTarget.Name&#39; to &#39;logfile&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.Targets.FileTarget.Header&#39; to &#39;********* ${processname} Assembly: ${gdc:item=assemblyName} Version: ${gdc:item=version} Started @ ${longdate} *********&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;assemblyName&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;version&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.Targets.FileTarget.FileName&#39; to &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${gdc:item=assemblyName}${date:format=HHmmss}.log&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.LayoutRenderers.GdcLayoutRenderer.Item&#39; to &#39;assemblyName&#39;
2023-05-25 08:49:31.3522 Debug Setting &#39;NLog.LayoutRenderers.DateLayoutRenderer.Format&#39; to &#39;HHmmss&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.Targets.FileTarget.Layout&#39; to &#39;${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}}&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ProcessInfoLayoutRenderer.Property&#39; to &#39;StartTime&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ProcessInfoLayoutRenderer.Format&#39; to &#39;HHmmss.ffff&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.Wrappers.CachedLayoutRendererWrapper.Cached&#39; to &#39;true&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper.Inner&#39; to &#39;${level}&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.Wrappers.OnExceptionLayoutRendererWrapper.Inner&#39; to &#39; EXCEPTION OCCURRED:${exception:format=ToString,Data:maxInnerExceptionLevel=10:innerExceptionSeparator=String:separator=String:exceptionDataSeparator=string}&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ExceptionLayoutRenderer.Format&#39; to &#39;ToString,Data&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ExceptionLayoutRenderer.MaxInnerExceptionLevel&#39; to &#39;10&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ExceptionLayoutRenderer.InnerExceptionSeparator&#39; to &#39;String&#39;
2023-05-25 08:49:31.3671 Debug Setting &#39;NLog.LayoutRenderers.ExceptionLayoutRenderer.Separator&#39; to &#39;String&#39;
2023-05-25 08:49:31.3820 Debug Setting &#39;NLog.LayoutRenderers.ExceptionLayoutRenderer.ExceptionDataSeparator&#39; to &#39;string&#39;
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 &#39;NLog.config&#39; 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 &gt;= Trace for Logger: ProcessCeptorStreamingOutput
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Trace] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Debug] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Info] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Warn] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Error] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Fatal] =&gt; 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 &gt;= Trace for Logger: ProcessCeptorStreamingOutput
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Trace] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Debug] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Info] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Warn] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Error] =&gt; logfile
2023-05-25 08:49:31.4600 Debug Logger ProcessCeptorStreamingOutput [Fatal] =&gt; logfile
2023-05-25 08:49:31.5341 Debug FileTarget(Name=logfile): Preparing for new file: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log&#39;
2023-05-25 08:49:31.5341 Debug FileTarget(Name=logfile): Creating file appender: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log&#39;
2023-05-25 08:49:37.6631 Debug FileTarget(Name=logfile): Preparing for new file: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log&#39;
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): Creating file appender: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log&#39;
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: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084931.log&#39;
2023-05-25 08:49:37.6639 Debug FileTarget(Name=logfile): FileAppender Invalidate Closing File: &#39;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/2023-05-25/ProcessCeptorStreamingOutput084937.log&#39;
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 &#39;C:\SoftwareDevel\Div8\CEPTOR\ProcessCeptorStreamingOutput\ProcessCeptorStreamingOutput\bin\x64\Debug\net7.0&#39; filter &#39;NLog.config&#39;
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:

&lt;targets&gt;
&lt;target xsi:type=&quot;File&quot; name=&quot;logfile&quot;
fileName=&quot;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss:cached=true}.log&quot; /&gt;
&lt;/targets&gt;

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:

    &lt;targets&gt;
&lt;target xsi:type=&quot;File&quot; name=&quot;logfile&quot;
fileName=&quot;C:/Users/Public/Documents/RSA/ProcessCeptorStreamingOutput/Logs/${shortdate}/${appName}${date:format=HHmmss:cached=true}.log&quot; /&gt;
&lt;/targets&gt;

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

huangapple
  • 本文由 发表于 2023年5月24日 23:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定