如何使用NLog作为DLL [英] How to use NLog for a DLL

查看:237
本文介绍了如何使用NLog作为DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Nlog Refresh 1.0为类库项目实现一个简单的日志。
似乎nlog在dll中实例化时不会创建一个日志文件。



有没有其他的方法?



我的配置文件如下所示:

 < nlog xmlns =http://www.nlog-project.org/schemas/NLog.xsd
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
throwExceptions = 真 >

< targets>
< target name =filexsi:type =FilefileName =$ {basedir} /nlog.txt/>
< target name =consolexsi:type =Console/>
< / targets>

< rules>
< logger name =*minlevel =InfowriteTo =file/>
< logger name =*minlevel =InfowriteTo =console/>
< / rules>

< / nlog>

我知道这个配置没有任何错误,因为它在一个exe项目中工作。

编辑:
只是为了澄清:
我无法访问使用我的dll作为插件的调用程序。调用程序实际上是使用我的dll作为插件的outlook。我想要保留一个只与我的DLL有关的日志,与outlook本身无关。

解决方案

你有将nlog.config添加到使用dll的exe文件的位置!



编辑:
您不必修改exe文件将nlog.config放在同一个目录下,如果没有选项,我想你将不得不从代码 https://github.com/nlog/NLog/wiki/Configuration-API


I am trying to implement a simple log using Nlog Refresh 1.0 for a class Library project. It seems nlog does not create a logfile when it's instantiated from within a dll.

Is there some other way around this ?

my config file looks like this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true">

    <targets>
      <target name="file" xsi:type="File" fileName="${basedir}/nlog.txt" />
        <target name="console" xsi:type="Console" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
        <logger name="*" minlevel="Info" writeTo="console" />
    </rules>

</nlog>

I know there is nothing wrong with this config because it works from within an exe project.

Edit: just to clarify: I have no access to the calling program which uses my dll as a plugin. The calling program is in fact outlook which uses my dll as a plugin. I would want to keep a log which is only relevant to my dll and has nothing to do with outlook itself.

解决方案

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

这篇关于如何使用NLog作为DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆