【问题标题】:How to configure NLog (Servicestack) for Multiple files如何为多个文件配置 NLog (Servicestack)
【发布时间】:2015-05-06 04:01:13
【问题描述】:

我需要为每个运行的线程保存一个日志文件。

所以我想要不同的日志文件,下面的代码保存了一个日志,但是我需要创建不同的日志,如何调用方法说我要保存哪个文件?

LogManager.LogFactory = new NLogFactory();
var log = LogManager.GetLogger(typeof(Program));
log.Info("********************* TASK REPLICATOR STARTED *********************");

所以我需要配置类似的东西:

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
    <targets>
      <target name="logging" xsi:type="File" fileName="${basedir}/logs/${level}.log" archiveFileName="${basedir}/logs/archives/${level}.{###}.log" archiveAboveSize="1048576" archiveNumbering="Sequence" maxArchiveFiles="20" concurrentWrites="true" layout="${longdate}|${callsite}|${level}|${message}" />
      <target name="exception" xsi:type="File" fileName="${basedir}/logs/${level}.log" archiveFileName="${basedir}/logs/archives/${level}.{###}.log" archiveAboveSize="1048576" archiveNumbering="Sequence" maxArchiveFiles="20" concurrentWrites="true" layout="${longdate}|${message}|${exception:format=tostring}" />
      <!-- THIS LINE BELOW DOES WHAT I NEED? -->
      <target name="mynewlogfile" xsi:type="File" fileName="${basedir}/logs/mynewlogfile.log" archiveFileName="${basedir}/logs/archives/TransactionTypes.{###}.log" archiveAboveSize="1048576" archiveNumbering="Sequence" maxArchiveFiles="20" concurrentWrites="true" layout="${longdate}|${callsite}|mynewlogfile|${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" maxlevel="Warn" writeTo="logging" />
      <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="exception" />
    </rules>
  </nlog>

【问题讨论】:

    标签: c# servicestack nlog


    【解决方案1】:

    您应该能够在文件名布局中简单地使用${threadid}(或者如果您将线程命名为${threadname})。

    这将自动将日志条目分隔到每个线程的一个文件中。

    <target name="mynewlogfile" xsi:type="File" 
            fileName="${basedir}/logs/mynewlogfile-thread-${threadid}.log" 
            ...
            layout="${longdate}|${callsite}|mynewlogfile|${message}" />
    

    【讨论】:

    • 难以置信你是个传奇!我在哪里可以找到它的文档?
    • 没问题! This 是文件目标的页面,它告诉您fileName 是一个布局。 this 是您可以在布局中使用的“布局渲染器”(${something} 标记)列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多