How to use NLog for a DLL

微笑、不失礼 提交于 2019-12-03 23:53:31
Peter

you have to add the nlog.config to the location of the exe file that uses the dll!

Edit: You don't have to modify the exe file just place the nlog.config in the same directory, if that is no option i guess you will have to configure it from code https://github.com/nlog/NLog/wiki/Configuration-API

TheWalkingPanda

NLog will look for a configuration file from the exe that includes your DLL. So you will need to copy a configuration file for NLog everytime you want to use your DLL.

In theory, this is the correct design pattern as many third party applications can use your DLL in different ways and would like to have your DLL's logs centralized with their logs.

In my case (and maybe your), we wanted to make sure 100% that the logs of the DLL are saved using our own configuration file, to make sure that all fatal errors are reported to our SqlServer etc. Hence we don't want the third party application to set its own logging logic.

We had the same issue where NLog (or Log4Net) couldn't find the configuration file, thus no logging activity was started and no exception / error message was printed from NLog or Log4Net.

The only alternative is to configure NLog (or Log4Net) in your DLL using code. NLog provides you with a Configuration API so that you can ditch the configuration files. See NLog configuration API

In my case, I have a static class called 'Utils' in my DLL, which is called from any third-party class that uses my DLL (e.g. they do Utils.doSomething()). So I configure NLog in a static constructor:

static Utils() // static constructor
{
    // Step 1. Create configuration object 
    var config = new LoggingConfiguration();

    // Step 2. Create targets and add them to the configuration 
    var consoleTarget = new ColoredConsoleTarget();
    config.AddTarget("console", consoleTarget);

    var fileTarget = new FileTarget();
    config.AddTarget("file", fileTarget);

    // Step 3. Set target properties 
    consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}";
    fileTarget.FileName = @"C:\global-logs\logs.txt";
    fileTarget.Layout = "${message}";
    // Step 4. Define rules
    var rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
    config.LoggingRules.Add(rule1);

    var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
    config.LoggingRules.Add(rule2);

    // Step 5. Activate the configuration
    LogManager.Configuration = config; 
}

you should put the piece of configuration file as above in the web.config for web applications or in the app.config for windows applications. This is not done automatically and if your class library has an app.config this is anyway not used when the class library gets called by another application.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!