简体   繁体   English

NLog 基于静态变量创建日志文件

[英]NLog create log files based on static variable

How can I set NLog file name based on static variable in my application.如何根据应用程序中的静态变量设置 NLog 文件名。

I have windows service that performs different tasks.我有执行不同任务的 Windows 服务。 Reads configuration file with task details.读取包含任务详细信息的配置文件。 I would like to create log file based on given task name.我想根据给定的任务名称创建日志文件。

NOTE: class name will not work, since all the tasks call the same code.注意:类名不起作用,因为所有任务都调用相同的代码。

NOTE: I am already using ${logger} variable as my current class.注意:我已经在使用 ${logger} 变量作为我当前的类。 Since I need to know where I am as well.因为我也需要知道我在哪里。

-------------UPDATE-------------- - - - - - - -更新 - - - - - - -

Seems like this is not possible to do.似乎这是不可能做到的。 Modified question: How to set variable values at run time?修改问题:如何在运行时设置变量值? I am talking about this:我正在谈论这个:

<variable name="logFileName" value="" />

Thank you.谢谢你。

You cannot, I believe.你不能,我相信。 But you can use something like this:但是你可以使用这样的东西:

In code:在代码中:

static NLog.Logger loggerA = NLog.LogManager.GetLogger("nameA");
static NLog.Logger loggerB = NLog.LogManager.GetLogger("nameB");

void Something()
{
    loggerA.Error("Something");
}

void SomethingElse()
{
    loggerB.Error("SomethingElse");
}

NLog config: NLog 配置:

<nlog ...>
    <targets>
        <target name="Error" xsi:type="AsyncWrapper">
            <target name="file" xsi:type="File" fileName="${basedir}/Logs/Error.txt">
                <layout ... />
            </target>
        </target>
    </targets>
    <!--other targets pointing to different files.-->
    <rules>
        <logger name="nameA" minlevel="Warn" writeTo="Error" />
        <logger name="nameB" minlevel="Trace" maxLevel="Info" writeTo="Log" />-->
        <logger name="*" minlevel="Trace" maxLevel="Info" writeTo="CommonLog" />
    </rules>
</nlog>

You can also use SomeNamespace.Component.* as name of a logger and than only logs from SomeNamespace.Component will be logged via it.您还可以使用SomeNamespace.Component.*作为记录器的名称,并且只有来自SomeNamespace.Component日志将通过它记录。 In that case the logger would be obtained like this:在这种情况下,记录器将像这样获得:

static NLog.Logger loggerA = NLog.LogManager.GetCurrentClassLogger();

Here is the documentation for Nlog: https://github.com/NLog/NLog/wiki/Tutorial这是 Nlog 的文档: https : //github.com/NLog/NLog/wiki/Tutorial

There is a way to edit NLog configuration programmatically: https://github.com/nlog/NLog/wiki/Configuration-API有一种以编程方式编辑 NLog 配置的方法: https : //github.com/nlog/NLog/wiki/Configuration-API

The easy solution is to use NLog GlobalDiagnosticsContext:简单的解决方案是使用 NLog GlobalDiagnosticsContext:

<target name="file" xsi:type="File" fileName="${gdc:logFileName:whenEmpty=DefaultApp}.txt">

Then you can override it like this:然后你可以像这样覆盖它:

NLog.GlobalDiagnosticsContext.Set("logFileName", "HelloApp");

See also: https://github.com/nlog/nlog/wiki/Gdc-Layout-Renderer另见: https : //github.com/nlog/nlog/wiki/Gdc-Layout-Renderer

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

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