具有不同配置的 NLog 记录器

发布于 2024-11-24 23:02:15 字数 122 浏览 1 评论 0原文

在 NLog 中可以创建具有不同配置的多个记录器吗?

我有一个组件,每次实例化时都必须将所有事件记录到与新实例相关的不同文件中。

NLog 可以做到这一点吗?如果没有,有日志框架可以做到这一点吗?

In NLog is possible to create multiple loggers with different configuration?

I have a component that every time that is instanced must log all events to a different file that is related to the new instance.

Is this possible with NLog? If not, there are logging frameworks that do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-12-01 23:02:15

是的,你可以这样做。您可以配置该类型的记录器以记录到特定目标。或者,您可以配置该类型的记录器以记录到目标(例如文件),并根据记录器名称(自动)命名文件。

有关一些示例,请参阅NLog 配置文件文档

另外,请参阅我的此处的帖子了解一些配置文件提示。

下面是一个非常简短的示例,说明如何配置两个记录器:一个用于将特定类型记录到以该类型命名的输出文件中,另一个用于所有其他记录器根据日期记录到文件中。

<nlog>
  <targets> 
    <target name="f1" xsi:type="File" fileName="${logger}.txt" />
    <target name="f2" xsi:type="File" fileName="${shortdate}.txt" />
  </targets>
  <rules>
    <logger name="Name.Space.Class1" minlevel="Trace" writeTo="f1" />  
    <logger name="*" levels="Debug" writeTo="f2" />
  </rules>
</nlog>

如果您希望类型 Name.Space.Class1 的日志转到“特殊”文件(即名称由记录器确定的文件),那么您可以将“final”添加到记录器规范中,如下所示:

<logger name="Name.Space.Class1" minlevel="Trace"final="true" />

Yes, you can do that. You can either configure the logger for that type to log to a specific target. Or you can configure the logger for that type to log to a target (such as a file), naming the file (automatically) based on the logger name.

See the NLog config file documentation here for some examples.

Also, see my post here for some config file tips.

Here is a very brief example of how you might configure two loggers: one for a specific type to be logged to an output file named for that type and one for all other loggers to log to a file based on the date.

<nlog>
  <targets> 
    <target name="f1" xsi:type="File" fileName="${logger}.txt" />
    <target name="f2" xsi:type="File" fileName="${shortdate}.txt" />
  </targets>
  <rules>
    <logger name="Name.Space.Class1" minlevel="Trace" writeTo="f1" />  
    <logger name="*" levels="Debug" writeTo="f2" />
  </rules>
</nlog>

If you want the logs for type Name.Space.Class1 to go to the "special" file (i.e. the one whose name is determined by the logger), then you can add "final" to the logger specfication like this:

<logger name="Name.Space.Class1" minlevel="Trace"final="true" />
忘你却要生生世世 2024-12-01 23:02:15

我的 NLog.config 完整示例

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="false" internalLogFile="\\YOURSERVNAME\Logs\nlog-internal.log" internalLogLevel="Warn">
  <variable name="basedir" value="${basedir}/Logs"></variable>
  <targets>
    <default-target-parameters xsi:type="File" fileName="${var:basedir}/Trace.csv" archiveFileName="${var:basedir}/Archives/Trace/Trace.{##}.csv" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd" archiveEvery="Month" maxArchiveFiles="15" archiveAboveSize="10485760" keepFileOpen="false"/>
    <default-wrapper xsi:type="BufferingWrapper" bufferSize="500" flushTimeout="10000" />
    <target name="TraceLog" xsi:type="File">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
    <target name="ErrorLog" xsi:type="File" fileName="${var:basedir}/Errors.csv"  archiveFileName="${var:basedir}/Archives/Errors/Errors.{##}.csv" maxArchiveFiles="10">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="TraceLog" />
    <logger name="*" minlevel="Error" writeTo="ErrorLog" />
  </rules>
</nlog>

My full example of NLog.config

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="false" internalLogFile="\\YOURSERVNAME\Logs\nlog-internal.log" internalLogLevel="Warn">
  <variable name="basedir" value="${basedir}/Logs"></variable>
  <targets>
    <default-target-parameters xsi:type="File" fileName="${var:basedir}/Trace.csv" archiveFileName="${var:basedir}/Archives/Trace/Trace.{##}.csv" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd" archiveEvery="Month" maxArchiveFiles="15" archiveAboveSize="10485760" keepFileOpen="false"/>
    <default-wrapper xsi:type="BufferingWrapper" bufferSize="500" flushTimeout="10000" />
    <target name="TraceLog" xsi:type="File">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
    <target name="ErrorLog" xsi:type="File" fileName="${var:basedir}/Errors.csv"  archiveFileName="${var:basedir}/Archives/Errors/Errors.{##}.csv" maxArchiveFiles="10">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="TraceLog" />
    <logger name="*" minlevel="Error" writeTo="ErrorLog" />
  </rules>
</nlog>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文