Microsoft.Extensions.Logging的记录器包装程序的实现和使用

编程入门 行业动态 更新时间:2024-10-26 01:31:14
本文介绍了Microsoft.Extensions.Logging的记录器包装程序的实现和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此问题与史蒂文的答案-此处.他提出了一个很好的记录器包装器.我将在下面粘贴他的代码:

This question is related to Steven’s answer - here. He proposed a very good logger wrapper. I will paste his code below:

public interface ILogger { void Log(LogEntry entry); } public static class LoggerExtensions { public static void Log(this ILogger logger, string message) { logger.Log(new LogEntry(LoggingEventType.Information, message, null)); } public static void Log(this ILogger logger, Exception exception) { logger.Log(new LogEntry(LoggingEventType.Error, exception.Message, exception)); } // More methods here. }

所以,我的问题是创建替代Microsoft.Extensions.Logging的实现的正确方法是什么和稍后在代码中使用它的最佳方法是什么 >?

So, my question is what is the proper way to create implementation that proxies to Microsoft.Extensions.Logging and what is the best way to use it later in the code?

注意:此问题是关于log4net的问题的副本,但现在特定于Microsoft.Extensions.Logging.

Note: this question is a copy of this question about log4net but now specific to Microsoft.Extensions.Logging.

推荐答案

所以,我的问题是创建代理到Microsoft.Extensions.ILogger的实现的正确方法是什么?

So, my question is what is the proper way to create implementation that proxies to Microsoft.Extensions.ILogger?

您应该创建类似以下内容的

you should create something like:

public sealed class MicrosoftLoggingAdapter : ILogger { private readonly Microsoft.Extensions.ILogger adaptee; public MicrosoftLoggingAdapter (Microsoft.Extensions.ILogger adaptee) => this.adaptee = adaptee; public void Log(LogEntry e) => adaptee.Log(ToLevel(e.Severity), 0, e.Message, e.Exception, (s, _) => s); private static LogLevel ToLevel(LoggingEventType s) => s == LoggingEventType.Debug ? LogLevel.Debug : s == LoggingEventType.Information ? LogLevel.Information : s == LoggingEventType.Warning ? LogLevel.Warning : s == LoggingEventType.Error ? LogLevel.Error : LogLevel.Critical; }

稍后在代码中使用它的最佳方法是什么?

what is the best way to use it later in the code?

如果使用的是DI容器,则只需使用DI容器将ILogger映射到MicrosoftLoggingAdapter.您还需要注册Microsoft.Extensions.ILogger,或者只是将MS记录器的实例提供给DI容器,以将其注入到MicrosoftLoggingAdapter构造函数中.

If you are using a DI container, then just use the DI container to map ILogger to MicrosoftLoggingAdapter. You also need to register Microsoft.Extensions.ILogger, or just give an instance of MS logger to the DI container to inject it to the MicrosoftLoggingAdapter constructor.

如果您不使用DI容器,即使用纯DI ,然后您将执行以下操作:

If you don't use a DI container, i.e., you use Pure DI, then you do something like this:

var logger = loggerFactory.CreateLogger("Application"); ILogger logging_adapter = new MicrosoftLoggingAdapter(logger); var myobject = new MyClass(other_dependencies_here, logging_adapter);

更多推荐

Microsoft.Extensions.Logging的记录器包装程序的实现和使用

本文发布于:2023-11-15 04:09:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记录器   程序   Microsoft   Extensions   Logging

发布评论

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

>www.elefans.com

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