小编min*_*oft的帖子

如何在引用的类库中使用 serilog 并与 CallerMember、CallerFilePath(不是 asp.net 核心)一起使用

我正在开发一个 WPF 应用程序,可以成功地设置 serilog 并通过将它放在 App.xaml.cs 中来访问它的静态方法,

但是,我还想登录项目中各种引用的类库。

由于循环依赖,我无法从类库本身引用主应用程序,因此在这些类库中执行日志记录的最佳方法是什么?

public class LogManager : IMLogManager
    {
        #region Constructor

        public LogManager()
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .Enrich.FromLogContext()
                         .WriteTo.Console(LogEventLevel.Debug, OutputTemplate, theme: AnsiConsoleTheme.Code)
                         .WriteTo.Async(a => a.File("./log/log.log",
                                                    LogEventLevel.Debug,
                                                    OutputTemplate,
                                                    rollingInterval: RollingInterval.Day,
                                                    encoding: Encoding.UTF8,
                                                    buffered: true,
                                                    rollOnFileSizeLimit: true))
                         .CreateLogger();
        }

        #endregion

        #region Private properties

        /// <summary>
        ///     Template ghi log.
        /// </summary>
        public static string OutputTemplate =>
            "[{Timestamp:dd-MM-yyyy HH:mm:ss.fff} {Level:u3}] source {SourceContext} ::: {Message:lj}{NewLine}{Exception}{NewLine}";

        #endregion

        #region Private methods

        private static string FormatMessage(string message,
                                            string memberName …
Run Code Online (Sandbox Code Playgroud)

.net c# serilog

2
推荐指数
1
解决办法
3180
查看次数

标签 统计

.net ×1

c# ×1

serilog ×1