没有为LoggerProvider设置LogLevel

时间:2018-09-05 03:15:01

标签: c# logging .net-core

我正在使用自定义LoggerProvider将日志写入文件。我的提供程序运行良好,但是我不能将日志级别设置为小于“信息”。

我正在使用loggerFactory.AddConsole(minLevel:LogLevel.Debug);来设置日志级别,但这不起作用。我可以看到所有事件-“警告”,“信息”等,但看不到“调试”和“跟踪”。我的ILogger中的MinLevel是“信息”。

如何更改LoggerProvider中的级别?

我的ILogger:

public class FileLogger : ILogger
        {
            private string filePath;
            private object _lock = new object();

            private readonly Func<string, LogLevel, bool> _filter;

            public FileLogger(string path)
            {
                filePath = path;
            }
            public IDisposable BeginScope<TState>(TState state)
            {
                return null;
            }

            public bool IsEnabled(LogLevel logLevel)
            {
                return true;
            }

            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                if (formatter != null)
                {
                    lock (_lock)
                    {
                        switch (logLevel)
                        {
                            case LogLevel.Debug:
                                {
                                    File.AppendAllText(filePath + System.DateTime.Now.ToShortDateString() + "_DEBUG.log", DateTime.Now.ToString("T") + "   " + formatter(state, exception) + Environment.NewLine);
                                    break;
                                }
                            case LogLevel.Critical:
                                {
                                    File.AppendAllText(filePath + System.DateTime.Now.ToShortDateString() + "_CRITICAL.log", DateTime.Now.ToString("T") + "   " + formatter(state, exception) + Environment.NewLine);
                                    break;
                                }
                            default:
                                {
                                    File.AppendAllText(filePath + System.DateTime.Now.ToShortDateString() + ".log", DateTime.Now.ToString("T") + "   " + formatter(state, exception) + Environment.NewLine);
                                    break;
                                }
                        }


                    }
                }
            }
        }

我的ILoggerProvider:

 public class FileLoggerProvider : ILoggerProvider
        {
            private string path;

            public FileLoggerProvider(string _path)
            {
                path = _path;
            }

            public ILogger CreateLogger(string categoryName)
            {
                return new FileLogger(path);
            }

            public void Dispose()
            {
            }
        }

和LoggerExtensions:

public static class FileLoggerExtensions
    {
        public static ILoggerFactory AddFile(this ILoggerFactory factory,
                                        string filePath)
        {
            factory.AddProvider(new Values.FileLoggerProvider(filePath));
            return factory;
        }
    }

我在configure.cs中的代码

    loggerFactory.AddFile("C:/log/");
    loggerFactory.AddConsole(minLevel:LogLevel.Debug);
    loggerFactory.AddDebug(minLevel: LogLevel.Debug);
    var _logger = loggerFactory.CreateLogger("FileLogger");

0 个答案:

没有答案