简体   繁体   中英

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

<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.

When creating the first NLog Logger-object, then NLog will attempt to load the NLog.config (or app.config) automatically. Searching multiple 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):

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. If wanting to have an isolated NLog configuration for your class-library, then you should consider creating an isolated NLog LogFactory:

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). Then you can also do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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