如何为主机应用程序中的隔离插件配置NLog

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

How to configure NLog for isolated plugin within host application

问题

I'm working on a C# application, using the NLog library.

Log.Info() generates an output, which looks like the following:

2023-07-24 09:04:19.4423 | Info | Company.Customer.Manager.DoSomething | Some information

I would like to change that, and in the NLog.xml, I have found entries like:

<code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>

... but there are some drawbacks:

  1. I have found over 30 of those entries in NLog.xml, belonging to members with following names: M:NLog.Targets.TargetWithLayout.#ctor, P:NLog.Targets.TargetWithLayout.Layout, M:NLog.Targets.NetworkTarget.#ctor, and so on.
  2. The entries have the following structure:
<member name="...">
    <summary>
        Some information
    </summary>
    <remarks>
        The default value of the layout is: 
        <code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>
    </remarks>
</member>

=> basically it looks "just" like a remark.

So my questions are:

  • Is it correct that the configuration of the NLog is somewhere in the remark of a member?
  • If yes, how can I know which member?
  • If no, where can I find that configuration?

Oh, before I forget: I don't have an NLog.config file.

英文:

I'm working on a C# application, using the NLog library.

Log.Info() generates an output, which looks like the following:

2023-07-24 09:04:19.4423 | Info | Company.Customer.Manager.DoSomething | Some information

I would like to change that, and in the NLog.xml, I have found entries like:

<code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>

... but there are some drawbacks:

  1. I have found over 30 of those entries in NLog.xml, belonging to members with following names: M:NLog.Targets.TargetWithLayout.#ctor, P:NLog.Targets.TargetWithLayout.Layout, M:NLog.Targets.NetworkTarget.#ctor, and so on.
  2. The entries have the following structure:
<member name="...">
    <summary>
        Some information
    </summary>
    <remarks>
        The default value of the layout is: 
        <code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>
    </remarks>
</member>

=> basically it looks "just" like a remark.

So my questions are:

  • Is it correct that the configuration of the NLog is somewhere in the remark of a member?
  • If yes, how can I know which member?
  • If no, where can I find that configuration?

Oh, before I forget: I don't have an NLog.config file.

答案1

得分: 1

将一个名为 "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">

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
        <target name="logconsole" xsi:type="Console" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="logconsole" />
        <logger name="*" minlevel="Debug" writeTo="logfile" />
    </rules>
</nlog>

然后,您可以开始进行更改,例如更改目标的 "layout":

<target 
  layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
  name="logconsole" 
  xsi:type="Console" />

有关可以在布局中使用的标签列表,请参阅 布局渲染器

还有其他处理配置的方式,例如将整个 nlog 标记放在您的 app.config 文件中。但是,app.config 可能会变得有点臃肿,因此我认为最好使用单独的文件。

英文:

Add a nlog.config -file to your project. Set the properties of the file to "Copy if Newer" to ensure it is copied to the output directory.

Then copy the some example configuration to your file. For example the one from the configuration file wiki

&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;&gt;

    &lt;targets&gt;
        &lt;target name=&quot;logfile&quot; xsi:type=&quot;File&quot; fileName=&quot;file.txt&quot; /&gt;
        &lt;target name=&quot;logconsole&quot; xsi:type=&quot;Console&quot; /&gt;
    &lt;/targets&gt;

    &lt;rules&gt;
        &lt;logger name=&quot;*&quot; minlevel=&quot;Info&quot; writeTo=&quot;logconsole&quot; /&gt;
        &lt;logger name=&quot;*&quot; minlevel=&quot;Debug&quot; writeTo=&quot;logfile&quot; /&gt;
    &lt;/rules&gt;
&lt;/nlog&gt;

You can then start making changes, like changing the "layout" of the target:

&lt;target 
  layout=&quot;${longdate}|${level:uppercase=true}|${logger}|${message}&quot;
  name=&quot;logconsole&quot; 
  xsi:type=&quot;Console&quot; /&gt;

For a list of tags you can use in your layout, see layout renderers.

There are other ways to handle configuration, like putting the entire nlog-tag inside your app.config file. But the app.config can become kind of bloated, so I think it is better to use a separate file.

答案2

得分: 1

创建一个作为主机应用程序中的客人存在的插件时,客人不应该更换客厅里的地毯。

插件不应尝试修改NLog配置,因为这可能会对主机应用程序产生不希望的副作用。当插件使用NLog.LogManager.GetCurrentClassLogger()时,其输出将取决于主机应用程序的NLog配置。

如果插件希望拥有自己的独立NLog配置,那么插件可以初始化自己的独立NLog LogFactory,而不是使用NLog.LogManager。另请参阅:https://github.com/NLog/NLog/wiki/Configure-component-logging#isolated-logfactory

NLog.xml文件仅包含用于Visual Studio Intellisense的XML文档。NLog.dll不使用NLog.xml进行任何操作。

英文:

When creating a plugin that will live as guest in the house of the host-application, then it is not good style for the guest to replace the carpet in the living-room.

The plugin should not attempt to modify the NLog-configuration, as it can give unwanted side-effects for the host-application. When the plugin uses NLog.LogManager.GetCurrentClassLogger() then its output will depend on the NLog-configuration for the host-application.

If the plugin wants to have its own isolated NLog-configuration, then the plugin can initialize its own isolated NLog-LogFactory. Instead of using the NLog.LogManager. See also: https://github.com/NLog/NLog/wiki/Configure-component-logging#isolated-logfactory

The NLog.xml-file only contains XML-docs for Visual Studio Intellisense. NLog.dll doesn't use NLog.xml for anything.

huangapple
  • 本文由 发表于 2023年7月24日 16:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752676.html
匿名

发表评论

匿名网友

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

确定