将所有ASP.NET Core日志记录重定向到单个NLog记录器

时间:2017-11-13 14:28:52

标签: asp.net nlog

我有一个ASP.NET项目,它将日志发送到NLog。

然而,在这个项目中,我有自己的NLog记录器,我想知道如何通过它来路由所有日志。

我想我不应该将NLog添加为记录器,但我应该找到一种方法来注册一个方法,每次ASP尝试记录任何东西时都会调用它。

如何实现这一目标?

这是创建记录器的代码:

import React, { Component } from "react";

import { TransitionGroup, Transition } from "react-transition-group";



class Test extends Component {


    componentWillAppear(cb) {
        console.log('componentWillAppear')
        cb()
    }

    render() {
        return <div> test </div>
    }
}

class App extends Component {
    render() {
        return (
        <TransitionGroup>
         <Test />
         </TransitionGroup>
         )
    }
}

export default App;

这也是ASP.net如何附加到nlog:

        // create the module name
        var ProcessName = Process.GetCurrentProcess().ProcessName;
        _ModuleName = ProcessName  + " (\"" + Oracle.GuessMyName() + "\")";

        // create the logger configuration
        var Configuration = new LoggingConfiguration();

        // create the file target
        var FileTarget = new FileTarget ("file")
        {
            FileName            = @"x:\Logs\${processname}.log",
            ArchiveFileName     = @"x:\Logs\${processname}.{#}.log",
            Layout              = @"${longdate}|${logger}|${level}|${message}${onexception:|Exception occurred:${exception:format=tostring}${newline}",
            ArchiveEvery        = FileArchivePeriod.Day,
            ArchiveNumbering    = ArchiveNumberingMode.Rolling,
            MaxArchiveFiles     = 7,
            ConcurrentWrites    = true
        };

        Configuration.AddTarget(FileTarget);


        // create the viewer target
        var ViewerTarget = new NLogViewerTarget ("viewer")
        {
            Layout              = @"${message}${onexception:${newline} --> Exception occurred\:${exception:format=tostring}",
            IncludeSourceInfo   = true,
            IncludeCallSite     = true,
            Address             = @"udp://127.0.0.1:9999"
        };

        Configuration.AddTarget(ViewerTarget);

        // set the rules
        Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, FileTarget));
        Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, ViewerTarget));

        // set the configuration
        LogManager.Configuration = Configuration;

        // create a new logger
        _Logger = LogManager.GetLogger(_ModuleName);

现在,对于两个进程,当前日志布局看起来像这样(每次重新启动进程时动物名称都会自动更改) log layout

所以这两个过程:shinobi和mouserun在这里都有自己的日志输出,但ASP的任何相关内容都是ASP的nlog实例,无论进程如何。

目标是让shinobi的ASP输出进入shinobi记录器和mouserun ASP输出进入mouserun记录器。

3 个答案:

答案 0 :(得分:1)

查看NLog.Extensions.Logging的代码,它注入了自己的自定义日志提供程序。

您可以执行相同操作并只包装global-logger对象:

https://github.com/NLog/NLog.Extensions.Logging/blob/e48d6cc54d9abd70d976066265c7992117cbac5a/src/NLog.Extensions.Logging/NLogLoggerProvider.cs

https://github.com/NLog/NLog.Extensions.Logging/blob/1474ffe5b26d2ac95534ed01ef259133133bfb67/src/NLog.Extensions.Logging/NLogLoggerFactory.cs

https://github.com/NLog/NLog.Extensions.Logging/blob/2c05a4fbdda0fe026e60814d535e164e18786aef/src/NLog.Extensions.Logging/ConfigureExtensions.cs

    public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
    {
        ConfigureHiddenAssemblies();

        using (var provider = new NLogLoggerProvider(options))
        {
            factory.AddProvider(provider);
        }
        return factory;
    }

答案 1 :(得分:0)

您还可以创建自定义目标,并使用NLog规则将所有非全局记录器消息重定向到此目标:

https://github.com/nlog/NLog/wiki/Configuration-file#rules

然后,自定义目标可以将日志事件转发到global-logger:

https://github.com/NLog/NLog/wiki/How-to-write-a-custom-target

您应该小心循环日志记录。也许在custom-target中有一个过滤器来忽略来自global-logger的消息。

但我认为这是一个丑陋的解决方案,我无法理解只有一个记录器对象的限制。特别是当原因是它应该以应用程序命名。为什么不是名称的全局变量而不是滥用logger-name?

答案 2 :(得分:0)

备选您可以创建一个自定义目标包装器,它将Logger修复为LogEventInfo,因此当转发到包装目标(UDP- / File-target)时,它们看起来都来自同一个记录器。

与这个家伙试图做的相似:

https://github.com/NLog/NLog/issues/2352

再次非常丑陋的解决方案,并且只应该在无法弄清楚的时候使用,如何避免在所需的Nlog-targets配置中使用logger-name(例如使用其他东西配置文件-target-filename)