Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 特定loggername的Nlog规则不起作用_C#_Nlog - Fatal编程技术网

C# 特定loggername的Nlog规则不起作用

C# 特定loggername的Nlog规则不起作用,c#,nlog,C#,Nlog,我是Nlog的新手,所以我的问题可能很简单。但是我不能让它工作。 问题: 我的应用程序为几个客户做了一些事情。我想为每个客户提供单独的日志文件。所有配置必须按程序进行。 目前我有: private static Logger InitialiseLog(Target target, string customerName) { var loggerName = customerName == null? "globalLog" : customerName; var config

我是Nlog的新手,所以我的问题可能很简单。但是我不能让它工作。 问题: 我的应用程序为几个客户做了一些事情。我想为每个客户提供单独的日志文件。所有配置必须按程序进行。 目前我有:

private static Logger InitialiseLog(Target target, string customerName)
{
    var loggerName = customerName == null? "globalLog" : customerName;
    var config = LogManager.Configuration ?? new LoggingConfiguration();
    config.AddTarget("eventlog", target);
    var rule = new LoggingRule("*", LogLevel.Debug, target);
    config.LoggingRules.Add(rule);
    LogManager.Configuration = config;
    Logger _logger;
    _logger = LogManager.GetLogger(loggerName);

    return _logger;
}
其名称为:

var target =
    new FileTarget
    {
        FileName = customerName + ".log",
        Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}${exception}"
    };

    return InitialiseLog(target, customerName);
它可以工作,但对每个客户日志文件都写入相同的内容。 我想将单个信息写入特定文件,因此我将规则更改为:

var rule = new LoggingRule(loggerName, ...
但它不起作用,什么都没有写。 我的目标是实现这样的目标: LogManager.GetLogger(“Customer1”);->它只写入Customer1.log文件 LogManager.GetLogger(“globallo”);->它只写入globallo.log文件


我做错了什么?

我猜您希望同一个应用程序实例处理多个客户日志文件

也许你可以这样做:

private static Logger InitialiseLog(Target target, string customerName)
{
    var loggerName = string.IsNullOrEmpty(customerName) ? "globalLog" : customerName;

    var config = LogManager.Configuration ?? new LoggingConfiguration();
    if (config.FindTargetByName("eventlog")==null)
    {
        config.AddTarget("eventlog", target);
        var rule = new LoggingRule("*", LogLevel.Debug, target);
        config.LoggingRules.Add(rule);
        LogManager.Configuration = config;
    }

    return LogManager.GetLogger(loggerName);
}
我不知道您如何配置(或分配)NLog目标,但由于您隐藏了该信息,因此我猜NLog目标仅依赖于
${logger}
-名称,并且您可以对所有记录器使用相同的NLog目标

为每个记录器注册新目标的替代解决方案:

private static Logger InitialiseLog(Target target, string customerName)
{
    var loggerName = string.IsNullOrEmpty(customerName) ? "globalLog" : customerName;
    var targetAlias = "eventlog_" + loggerName;

    var config = LogManager.Configuration ?? new LoggingConfiguration();
    if (config.FindTargetByName(targetAlias)==null)
    {
        config.AddTarget(targetAlias, target);
        config.AddRule(LogLevel.Debug, LogLevel.Fatal, target, loggerName, true):
        if (LogManager.Configuration != null)
           LogManager.ReconfigExistingLoggers();
        else
           LogManager.Configuration = config;
    }

    return LogManager.GetLogger(loggerName);
}

可能customerName有空字符串?它将不起作用。这个方法被调用了几次。因此,如果我添加一个条件:config.FindTargetByName(“eventlog”)==null),这将仅在第一次运行时执行。如果您解释某些操作不起作用的原因,事情会容易得多。为什么多个记录器不能使用同一目标?