加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

全局声明log4net变量并在所有类文件中使用它们c#

发布时间:2020-12-16 01:27:56 所属栏目:百科 来源:网络整理
导读:我有一个用c#编写的类库.在这个库中,我们使用log4net来进行日志记录. 类库有2-3个类文件.这个类库也被一个用c#编写的 windows服务使用. 现在log4net已按以下方式实现. 1.我已经从nuget添加了log4net的引用. 2.在每个类文件中,将其添加到顶部: private stati
我有一个用c#编写的类库.在这个库中,我们使用log4net来进行日志记录.
类库有2-3个类文件.这个类库也被一个用c#编写的 windows服务使用.

现在log4net已按以下方式实现.
1.我已经从nuget添加了log4net的引用.
2.在每个类文件中,将其添加到顶部:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

3.想要打印一些额外的细节以及默认值,所以在log4net.config文件中添加了一些参数,如下所示:

<parameterName value="@Parameter1"/>
<parameterName value="@Parameter2"/>

并在布局模式中:

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date{dd/MM/yy HH:mm:ss} [%thread] %level  :: Parameter1 = %property{Parameter1} :: Parameter2 = %property{Parameter2}:: %logger : %message%newline"   />
</layout>

并在ClassA中的每个方法中添加了这一行,其中正在进行日志记录:

log4net.GlobalContext.Properties["Parameter1"] = parameter1;
log4net.GlobalContext.Properties["Parameter2"] = parameter2;

现在我面临的问题是,我的类库中有2个类文件,名为Class1和Class2.And参数1和参数2是ClassA中的公共变量.正如this问题中提到的,我无法在ClassB中访问paramter1和parameter2.因此,当打印classB方法的日志时,parameter1和parameter2始终为null.

那么有没有一种方法可以全局设置参数一次,每当打印日志时它就会使用相同的变量.

我的代码类似于以下内容:

public ClassA {
    public string parameter1 { get; set; } = "ABC"
    public string parameter2 { get; set; } = "XYZ"

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public void SomeMethod(){
        log4net.GlobalContext.Properties["Parameter1"] = parameter1;
        log4net.GlobalContext.Properties["Parameter2"] = parameter2;
        ClassB b = new ClassB();
        log.info("In Some Method");
        b.AnotherMethod();
        LogMessage("After AnotherMethod");  
    }   
}

public class ClassB(){
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public void AnotherMethod(){        
        log.info("Inside AnotherMethod");       
    }
}

预期产出:

30/10/18 21:57:11 [5] INFO  :: Parameter1 = 'ABC' :: Parameter2 = 'XYZ' :: ClassA : In Some Method
30/10/18 21:57:11 [5] INFO  :: Parameter1 = 'ABC' :: Parameter2 = 'XYZ' :: ClassB : Inside AnotherMethod
30/10/18 21:57:11 [5] INFO  :: Parameter1 = 'ABC' :: Parameter2 = 'XYZ' :: ClassA : After AnotherMethod

实际产量:

30/10/18 21:57:11 [5] INFO  :: Parameter1 = 'ABC' :: Parameter2 = 'XYZ' :: ClassA : In Some Method
30/10/18 21:57:11 [5] INFO  :: Parameter1 = (null) :: Parameter2 = (null) :: ClassB : Inside AnotherMethod
30/10/18 21:57:11 [5] INFO  :: Parameter1 = 'ABC' :: Parameter2 = 'XYZ' :: ClassA : After AnotherMethod

如何使参数1和2每次打印或全局声明.所以一旦我设置,它必须被所有log4net消息使用.

在这方面的任何帮助都非常感谢.

解决方法

代码中的这一行:

log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

最终得到2个不同的记录器,因此您在ClassA中为记录器设置的值与ClassB中的记录器不同.尝试将这两行改为:

log4net.LogManager.GetLogger("MyLogger");

它应该工作.

如果您阅读文档,您将看到当您调用使用类型名称加载记录器的this方法时“类型的全名将用作要检索的记录器的名称”.由于代码中有两种不同的类型,因此最终会有两种不同的记录器.您需要使用公共名称或公共类型来获取相同的记录器实例.

你的另一个选择是在设置文件上设置值,但我猜这对你不起作用,因为它们可能是你需要在运行时设置的值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读