简体   繁体   English

使用 serilog 在多个项目中使用相同的日志文件

[英]Same log file in multiple projects using serilog

I have a Logger.cs class:我有一个 Logger.cs 类:

public Logger(string logtype=null)
        {
            _logtype = logtype;
            LogEventLevel level = LogEventLevel.Warning;
            if (File.Exists(configPath))
            {
                XDocument xdoc = XDocument.Load(configPath);
                string val = xdoc.Descendants("logEnabled").First().Value;
                // if(clientConfig.LogEnabled == true)
                if (val == "true")
                {
                    level = LogEventLevel.Debug;

                }
                else if (val == "false")
                {
                    level = LogEventLevel.Warning;
                }
            }
            else
            {
                level = LogEventLevel.Warning;
            }
           _logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                         .WriteTo.File(_filepath, level)
                         .CreateLogger();

        }

this logger class is used in multiple projects in a solution.此记录器类用于解决方案中的多个项目。 Like shown below:如下图所示:

    Class A {
     Logger _logger = new Logger("A");
    }

Class A1 {
     Logger _logger = new Logger("A");
    }

Class B {
     Logger _logger = new Logger("B");
    }

Class B1 {
     Logger _logger = new Logger("A");
    }

With my above code.用我上面的代码。 Now i can see only the first logging is written to the log file.现在我只能看到第一个日志被写入日志文件。 Class A and Class A1 are using the same log file "A", when my program executes i can see only Class A logging is present in the log but not class A1 logging messages. A 类和 A1 类使用相同的日志文件“A”,当我的程序执行时,我只能看到日志中存在 A 类日志,而 A1 类日志消息中没有。 Same for Class B and Class B1, only class B messages are visible. B 类和 B1 类相同,只有 B 类消息可见。

How can i use the same log file across multiple project in a solution?如何在解决方案中的多个项目中使用相同的日志文件?

You will need to enable multi-process shared log files, set shared to true您将需要启用多进程共享日志文件,将shared设置为true

_logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File(_filepath, level, shared: true)
                .CreateLogger();

For more check Shared log files有关更多检查 共享日志文件

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

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