无法在配置源中找到“日志记录”的配置部分

时间:2015-01-23 06:31:05

标签: c# enterprise-library-6

我收到以下异常。我正在使用Enterprise Library 6.0 Application Logging Block。

    System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=The configuration section for Logging cannot be found in the configuration source.
  Source=Microsoft.Practices.EnterpriseLibrary.Logging
  StackTrace:
       at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory.LogWriterConfigurationBuilder.Create()
       at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory.Create()
       at Logger..ctor() in \MBSLogger.cs:line 19
       at EventLoggingTest.test_log_simple_exception() in c:\Users\Vinay.Joseph\Documents\Visual Studio 2013\EventLoggingTest.cs:line 20
  InnerException: 

代码库如下。

       IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
        LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
        Logger.SetLogWriter(logWriterFactory.Create());


        if (!Logger.IsLoggingEnabled())
        {

        }
        else { 
            Logger.Write(new LogEntry(){
                Severity = System.Diagnostics.TraceEventType.Information,
                TimeStamp = DateTime.Now,
                Message = "Hello World"
            });
        }

请告诉我哪里出错了。

2 个答案:

答案 0 :(得分:8)

app / web.config是什么样的? 你有日志配置设置吗?

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

答案 1 :(得分:0)

如果您已经在web.config文件中设置了配置部分,则它可能仍会失败,并出现异常,因为ConfigurationSourceFactory.Create()有时无法解析该部分。

可能的解决方法是使用

IConfigurationSource configurationSource = new FileConfigurationSourceFactory(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);