Logging.Logger不包含SetLogWriter的定义

时间:2019-03-28 16:06:44

标签: c# enterprise-library

此处复制的代码:https://www.c-sharpcorner.com/article/logging-block-in-microsoft-enterprise-library-6-0/

public class LoggerBlock
{
    protected LogWriter logWriter;
    public LoggerBlock()
    {
        InitLogging();
    }
    private void InitLogging()
    {
        logWriter = new LogWriterFactory().Create();
        Logger.SetLogWriter(logWriter, false);
    }
    public LogWriter LogWriter
    {
        get
        {
            return logWriter;
        }
    }
}

编译错误为:

  

Microsoft.Practies.EnterpriseLibrary.Logging.Logger不包含   “ SetLogWriter”的定义

对不起,我什至不知道我安装了哪个版本的库。我正在尝试向似乎已为Enterprise Library Logging设置app.config的现有程序中添加一些日志记录语句。

Intellisense仅提供以下类似方法:

Logger.SetContextItemLog (object key, object value) 

1 个答案:

答案 0 :(得分:1)

我猜您正在使用没有Logger.SetLogWriter()方法的旧版本。我刚刚安装了最新版本,并且您粘贴的代码运行良好。您可以更新使用的版本吗?

编辑:看来Logger.SetLogWriter()方法只是将默认的Logger设置为使用刚创建的LogWriter。直接使用创建的LogWriter代替默认的Logger怎么样?

var logWriter = new LogWriterFactory().Create();
logWriter.Write("Message");

以下文章可能还会为您提供更多信息。 Creating and Writing Log Entries