Serilog - 输出/丰富具有调用日志条目的 MethodName 的所有消息

问题描述 投票:0回答:6

有没有办法用方法名称来丰富所有 Serilog 输出。

例如,考虑如果我有以下情况;

Public Class MyClassName

  Private Function MyFunctionName() As Boolean
      Logger.Information("Hello World")
      Return True
  End Function

End Class

期望的输出如下;

2015-04-06 18:41:35.361 +10:00 [Information] [MyFunctionName] Hello World!

实际上完全限定名称会很好。

2015-04-06 18:41:35.361 +10:00 [Information] [MyClassName.MyFunctionName] Hello World!

“丰富器”似乎只适用于静态信息,并且每次都不起作用。

vb.net serilog
6个回答
44
投票

如果您需要 C# 版本:

public static class LoggerExtensions
{
    public static ILogger Here(this ILogger logger,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0) {
        return logger
            .ForContext("MemberName", memberName)
            .ForContext("FilePath", sourceFilePath)
            .ForContext("LineNumber", sourceLineNumber);
    }
}

像这样使用:

// at the beginning of the class
private static Serilog.ILogger Log => Serilog.Log.ForContext<MyClass>();

// in the method
Log.Here().Information("Hello, world!");

请记住在消息模板中添加这些属性。你可以使用这样的东西:

var outputTemplate = "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}in method {MemberName} at {FilePath}:{LineNumber}{NewLine}{Exception}{NewLine}";

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Warning()
            .Enrich.FromLogContext()
            .WriteTo.RollingFile("log/{Date}.log", outputTemplate, LogEventLevel.Warning)
            .WriteTo.Console(LogEventLevel.Warning, outputTemplate, theme: AnsiConsoleTheme.Literate)
            .CreateLogger();

20
投票

可以通过调用堆栈进行反射,使用丰富器来完成此操作,但这样做非常昂贵,因此 Serilog 不提供它。

您可以使用类似以下内容:

Logger.Here().Information("Hello, world!");

并实现

Here()
方法作为
ILogger
上的扩展方法:

<Extension>
Public Sub Here(ByVal logger as ILogger,
    <CallerMemberName> Optional memberName As String = Nothing)

    Return logger.ForContext("MemberName", memberName)
End Sub 

7
投票

基于 MovGP0 的回答(对于 C#),

我创建了一个解决方案,不需要在要记录的每一行中使用

Here()
-方法,只需将自定义
Log.cs
-类添加到项目的“根命名空间”中即可。

有关更多信息,请参阅:https://gist.github.com/litetex/b88fe0531e5acea82df1189643fb1f79


0
投票

在 Serilog 的 outputTemplate 中,配置日志写入

Properties
。方法名称将作为
ActionName
列的一部分写入。

ActionName
也可以在outputTemplate中单独配置(而不是所有属性)。

配置

Properties/ActionName
将以
Namespace.ClassName.MethodName
格式写入方法名称。


0
投票

C#版本可以简化。只需使用 AutofacSerilogIntegration 即可:

var path = Server.MapPath("~/");

        var outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} {Message} {NewLine}{Exception}";
        Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .WriteTo.File($"{path}/log/serilog-.log", LogEventLevel.Debug, outputTemplate, rollingInterval: RollingInterval.Day)
                    .CreateLogger();

        var builder = new ContainerBuilder();
        builder.RegisterLogger();  // from AutofacSerilogIntegration
        builder.RegisterControllers(typeof(MvcApplication).Assembly);
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

0
投票

似乎没有完美的方法来解决这个问题,但恕我直言,我不必在每个日志记录调用中都包含

Here()
,而是将以下行放在包含日志记录调用的方法的顶部:

using (LogContext.PushProperty(LogProperty.MethodName, CallContext.GetClassMethodName(this)));

LogProperty.MethodName
只是一个字符串,用于标识消息模板占位符中的自定义属性(在我的例子中,它是
MethodName
)。
CallContext.GetClassMethodName
方法的实现如下:

public static string GetClassMethodName
(
    object? caller = null,
    bool useFullName = false,
    [CallerMemberName] string? method = null
)
{
    return caller == null
        ? method ?? string.Empty
        : useFullName
            ? $"{caller.GetType().FullName}:{method}"
            : $"{caller.GetType().Name}:{method}";
}

请注意,我包含了类的全名或简称(因为我可能需要区分在不同类中实现的同名方法。并且此函数主要用于从实例方法进行日志记录。如果您只需要记录日志方法名称(或者想要从静态方法记录),可以使用以下辅助函数:

public static string GetMethodName
(
    [CallerMemberName] string? method = null
)
{
    return method ?? string.Empty;
}
© www.soinside.com 2019 - 2024. All rights reserved.