使用类名,方法名的Serilog日志记录

编程入门 行业动态 更新时间:2024-10-26 20:30:31
本文介绍了使用类名,方法名的Serilog日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Angular,Web API,EF项目中使用serilog和SEQ.我都是新来的.

I am using serilog and SEQ in my project Angular,web api, EF. I am new to both.

1)如何确保每次写入错误,信息时,调试它都应包含ClassName.Method名称.我知道我必须创建Enrich,但不确定如何获取ClassName.Method名称

1) How can I make sure every time I write error, information, debug it should contain ClassName.Method Name. I know I have to create Enrich but not sure how to get ClassName.Method Name

例如

Class Test { public void testing(){ // logger.Error("Error ....");} }

现在,当我看到日志时,它应该显示"29/09/2012 10:00:00,Test => testing Error ....."

Now,when I see the log it should display "29/09/2012 10:00:00, Test=>testing Error ....."

在简短的DateTime,ClassName,MethodName和Message中

In, short DateTime, ClassName , MethodName and Message

推荐答案

要在每个日志记录调用中自动获取方法名称,您必须使用可反映调用堆栈的扩展器(这样做非常昂贵)捕获方法名称.

To get the method name automatically on every logging call, you'll have to use an enricher that reflects over the call stack (which is very expensive to do) to capture the method name.

以下是 @nblumhardt 编写的示例: github/serilog/serilog/issues/1084#issuecomment-358117004

using System; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using Serilog; using Serilog.Configuration; using Serilog.Core; using Serilog.Events; namespace ConsoleApp24 { class CallerEnricher : ILogEventEnricher { public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { var skip = 3; while (true) { var stack = new StackFrame(skip); if (!stack.HasMethod()) { logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue("<unknown method>"))); return; } var method = stack.GetMethod(); if (method.DeclaringType.Assembly != typeof(Log).Assembly) { var caller = $"{method.DeclaringType.FullName}.{method.Name}({string.Join(", ", method.GetParameters().Select(pi => pi.ParameterType.FullName))})"; logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue(caller))); } skip++; } } } static class LoggerCallerEnrichmentConfiguration { public static LoggerConfiguration WithCaller(this LoggerEnrichmentConfiguration enrichmentConfiguration) { return enrichmentConfiguration.With<CallerEnricher>(); } } class Program { static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .Enrich.WithCaller() .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message} (at {Caller}){NewLine}{Exception}") .CreateLogger(); Log.Information("Hello, world!"); SayGoodbye(); Log.CloseAndFlush(); } [MethodImpl(MethodImplOptions.NoInlining)] static void SayGoodbye() { Log.Information("Goodbye!"); } } }

另一种替代方法(如果您只想在特定/更重要的地方捕获方法名称),是创建一个可以调用的扩展方法,该方法会将方法名称添加到日志记录上下文中,例如...

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

您可以在StackOverflow上的另一个问题上看到有关如何实现此 Here 方法的示例: stackoverflow/a/46905798

You can see an example of how to implement this Here method on a different question here on StackOverflow: stackoverflow/a/46905798

更多推荐

使用类名,方法名的Serilog日志记录

本文发布于:2023-11-11 23:50:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:法名   日志   Serilog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!