简体   繁体   English

带有类库的 Nlog

[英]Nlog with a class Library

I have a class library and configuration file in a separate place I want to use it so I do the following我在一个单独的地方有一个类库和配置文件我想使用它所以我执行以下操作

LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(configurationFile , true);
LogManager.ReconfigExistingLoggers();

The configuration of NLog is part of a remote app.config looks like NLog 的配置是远程 app.config 的一部分,看起来像

<configuration>
    <!-- ... -->
    <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    </configSections>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <targets>
            <!-- 
              Log in a separate thread, possibly queueing up to
              5000 messages. When the queue overflows, discard any
              extra messages
            -->
            <target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard" 
                layout="${machinename} - ${level} - ${longdate} - ${callsite} - ${threadid} - ${message} ${exception:format=tostring}">
                <target xsi:type="File" fileName="C:\logs/${level}.txt" />
            </target>
        </targets>
        <rules>
            <logger name="*" minlevel="Debug" writeTo="file" />
        </rules>
    </nlog>
</configuration>

the configuration object LogManager.Configuration always has the default values , any idea how to fix this.配置对象LogManager.Configuration总是有默认值,不知道如何解决这个问题。

When creating the first NLog Logger-object, then NLog will attempt to load the NLog.config (or app.config) automatically.创建第一个 NLog Logger 对象时,NLog 将尝试自动加载 NLog.config(或 app.config)。 Searching multiple locations:搜索多个位置:

https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-locations https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-locations

You should try and activate the NLog internal logger and see why it doesn't find the "remote app.config" without doing manually loading (Can be enabled by code):您应该尝试激活 NLog 内部记录器,看看为什么它在不进行手动加载的情况下找不到“远程 app.config”(可以通过代码启用):

https://github.com/NLog/NLog/wiki/Internal-Logging https://github.com/NLog/NLog/wiki/Internal-Logging

It is NOT recommended that the class library itself tries to modify the global NLog-configuration, as it might surprise the main-application.不建议类库本身尝试修改全局 NLog 配置,因为它可能会使主应用程序感到惊讶。 If wanting to have an isolated NLog configuration for your class-library, then you should consider creating an isolated NLog LogFactory:如果想要为您的类库设置一个独立的 NLog 配置,那么您应该考虑创建一个独立的 NLog LogFactory:

https://github.com/NLog/NLog/wiki/Configure-component-logging https://github.com/NLog/NLog/wiki/Configure-component-logging

If you want to load an NLog.config from a non-standard path (not app.config).如果要从非标准路径(不是 app.config)加载 NLog.config。 Then you can also do this:然后你也可以这样做:

https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM