“log4net.Core.LoggerManager”的类型初始值设定项引发异常

1、今天在使用log4net的时候,配置都完毕了,启动运行程序,

运行到以下代码实例化log对象的时候,出错,提示:“log4net.Core.LoggerManager”的类型初始值设定项引发异常

           log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

 

看了一下app.config配置,都没问题,后来把错误详细信息(如下)打开一看,才发现配置的时候,<configSections> 元素,必须是根 <configuration> 元素的第一个子元素。

改了以后,问题解决。

 

错误信息:

未处理 System.TypeInitializationException
  Message=“log4net.Core.LoggerManager”的类型初始值设定项引发异常。
  Source=log4net
  TypeName=log4net.Core.LoggerManager
  StackTrace:
       在 log4net.Core.LoggerManager.GetLogger(Assembly repositoryAssembly, String name)
       在 log4net.LogManager.GetLogger(Assembly repositoryAssembly, String name)
       在 log4net.LogManager.GetLogger(Type type)
       在 Program.Main(String[] args) 位置 E:\Program.cs:行号 16
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.TypeInitializationException
       Message=“log4net.Util.LogLog”的类型初始值设定项引发异常。
       Source=log4net
       TypeName=log4net.Util.LogLog
       StackTrace:
            在 log4net.Util.LogLog.Debug(String message)
            在 log4net.Core.LoggerManager..cctor()
       InnerException: System.Configuration.ConfigurationErrorsException
            Message=配置系统未能初始化
            Source=System.Configuration
            BareMessage=配置系统未能初始化
            Line=0
            StackTrace:
                 在 System.Configuration.ConfigurationManager.PrepareConfigSystem()
                 在 System.Configuration.ConfigurationManager.GetSection(String sectionName)
                 在 System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
                 在 System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
                 在 System.Diagnostics.DiagnosticsConfiguration.Initialize()
                 在 System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
                 在 System.Diagnostics.TraceInternal.InitializeSettings()
                 在 System.Diagnostics.TraceInternal.WriteLine(String message)
                 在 System.Diagnostics.Trace.WriteLine(String message)
                 在 log4net.Util.LogLog.EmitErrorLine(String message)
                 在 log4net.Util.LogLog.Error(String message, Exception exception)
                 在 log4net.Util.LogLog..cctor()
            InnerException: System.Configuration.ConfigurationErrorsException
                 Message=每个配置文件中只允许存在一个 <configSections> 元素,并且,如果存在该元素,它还必须是根 <configuration> 元素的第一个子元素。 (E:\★項目\4.物流\0.VSS物流\中邮云南供应链信息平台\02_source\SCM\SCM.Batch\BAT.AlarmManagement\AlarmBatch\bin\Release\BAT.AlarmManagement.vshost.exe.Config line 10)
                 Source=System.Configuration
                 BareMessage=每个配置文件中只允许存在一个 <configSections> 元素,并且,如果存在该元素,它还必须是根 <configuration> 元素的第一个子元素。
                 Filename=E:\★項目\4.物流\0.VSS物流\中邮云南供应链信息平台\02_source\SCM\SCM.Batch\BAT.AlarmManagement\AlarmBatch\bin\Release\BAT.AlarmManagement.vshost.exe.Config
                 Line=10
                 StackTrace:
                      在 System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
                      在 System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
                      在 System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
                      在 System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
                 InnerException:

2、运行没错了,但是后面又有问题了,就是log文件怎么跑都没见生成,程序运行也不出错,后来调查,原来是配置完以后,要正常使用,使用前通过下面方法需要加载配置:

          log4net.Config.XmlConfigurator.Configure();

加载的时候,web程序一般都是写在Global 的Application_Start里面,这样,web网站启动的时候就全局加载。 其它的winform或者控制台应用程序在program的开头加上即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值