如何在没有调用 ForContext 的特定约定的情况下启用 Serilog 最低级别覆盖? [英] How to enable Serilog minimum level overrides without particular convention of calling ForContext?

查看:34
本文介绍了如何在没有调用 ForContext 的特定约定的情况下启用 Serilog 最低级别覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇关于 Serilog 最低级别覆盖的文章指出:

Override 的第一个参数是源上下文前缀,它通常与与记录器关联的类的命名空间限定类型名称匹配.

The first argument of Override is a source context prefix, which is normally matched against the namespace-qualified type name of the class associated with the logger.

对于这种所谓的正常"行为,我是否不需要为调用我的记录器的每个类手动设置不同的 .ForContext<>() ?换句话说,如果没有关于如何设置 .ForContext 的特定约定,命名空间特定的最低日志级别应该如何工作?

For this so-called "normal" behavior, wouldn't I need to manually set the .ForContext<>() differently for each class my logger is called from? In other words, how are namespace-specific minimum log levels supposed to work without a specific convention of how .ForContext is set?

如果这是有道理的,那么如何自动设置 ForContext 而不用在任何地方使用不同的参数调用它?

If that makes sense, then how can I set ForContext automatically without calling it with a different argument everywhere?

推荐答案

对于这种所谓的正常"行为,难道我不需要手动设置.ForContext<>() 对于我的记录器调用的每个类都不同来自?

For this so-called "normal" behavior, wouldn't I need to manually set the .ForContext<>() differently for each class my logger is called from?

是的,你会的.一种常见的方法是在每个类上使用 Log.ForContext<T>(),在一个成员变量中,该成员变量在您的类的不同方法之间共享(以便所有日志都被写入具有相同的上下文).例如

Yes, you would. A common way of doing it is by using the Log.ForContext<T>() on each class, in a member variable that gets shared across the different methods of your class (so that all logs get written with the same context). e.g.

public class SomeService
{
    private readonly ILogger _log = Log.ForContext<SomeService>();
    // ...
}

public class SomeRepository
{
    private readonly ILogger _log = Log.ForContext<SomeRepository>();
    // ...
}

如果您使用的是 IoC 容器,例如 Autofac,您可以使用 .ForContext<>() 调用在 IoC 容器解析类时自动发生(例如,通过使用构造函数注入).

If you are using an IoC container such as Autofac, you can have the .ForContext<>() call happen automatically when classes are resolved by the IoC container (by using constructor injection, for example).

如果您专门使用 Autofac,您可以使用 AutofacSerilogIntegration 可以解决这个问题.其他 IoC 容器可能有类似的实现(或者您必须实现自己的).

If you are using Autofac specifically, you could use AutofacSerilogIntegration that takes care of that. There might be similar implementations for other IoC containers (or you'd have to implement your own).

如果您使用的是 Microsoft 的通用主机,则需要将其配置为使用自定义 ServiceProviderFactory 将负责创建实例并调用 .ForContext<>()... 一个简单的方法是将 Autofac 与 Microsoft 的通用主机集成 然后利用 AutofacSerilogIntegration I上面提到了.

If you are using Microsoft's Generic Host, then you'll need to configure it to use a custom ServiceProviderFactory which will be responsible for creating the instances and making the call to .ForContext<>()... An easy route is to integrate Autofac with Microsoft's Generic Host and then leverage the AutofacSerilogIntegration I mentioned above.

这篇关于如何在没有调用 ForContext 的特定约定的情况下启用 Serilog 最低级别覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆