Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我使用log4net执行此操作时,为什么会得到一个空的事件日志?_C#_Log4net_Event Log - Fatal编程技术网

C# 当我使用log4net执行此操作时,为什么会得到一个空的事件日志?

C# 当我使用log4net执行此操作时,为什么会得到一个空的事件日志?,c#,log4net,event-log,C#,Log4net,Event Log,我有一节课 namespace LogToolsTest { public class Foo { private static readonly ILog logger = LogManager.GetLogger(typeof(Foo)); public Foo() { logger.Debug("Save this text"); logger.Info("Save this

我有一节课

namespace LogToolsTest
{
    public class Foo
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Foo));

        public Foo()
        {

            logger.Debug("Save this text");
            logger.Info("Save this text");
            logger.Warn("Save this text");
            logger.Error("Save this text");
            logger.Fatal("Save this text");


            var b1 = logger.IsDebugEnabled;
            var b2 = logger.IsInfoEnabled;
            var b3 = logger.IsWarnEnabled;
            var b4 = logger.IsErrorEnabled;
            var b5 = logger.IsFatalEnabled;
        }
    }
}
我用简单的方法来测试这一点:

 Foo foo = new Foo();
和log4net.config


我将以下内容添加到我的assemblyinfo中:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
"App.config", Watch = true)]
执行此代码后,事件日志中没有条目。为什么呢

现在它几乎可以正常工作了: 我删除了log4net.config,并修改了app.config


但有点不对劲。我在输出中看到:

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   at System.Diagnostics.DiagnosticsConfiguration.Initialize()
   at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
   at System.Diagnostics.TraceInternal.InitializeSettings()
   at System.Diagnostics.TraceInternal.WriteLine(String message)
   at System.Diagnostics.Trace.WriteLine(String message)
   at log4net.Util.LogLog.EmitErrorLine(String message)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\toolkit\trunk\LogToolsTest\bin\Debug\LogToolsTest.vshost.exe.config line 3)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String key)

foreach appender.

您有权限问题。我只是把你的代码和一个小的调整添加了一个源,我知道我的用户有写访问权(MSSQLSERVER)

您将使用。完成后,您需要重新启动计算机

我的暗示是,有一句话说

log4net:EventLogAppender:Source[TestApplication.vshost.exe]已注册到日志[]

然后我把它改成了“MSSQLSERVER”

log4net:EventLogAppender:Source[MSSQLSERVER]已注册到log[Application]

启用调试的一些有用提示:


或者使用以下设置创建app.config:


您不需要调用XmlConfigurator.Configure(),因为您在assemblyinfo中放了一行。但是,如果您有一组程序集,需要记住的一件重要事情是,包含日志调用的第一个项目是其assemblyinfo需要该行的项目

换句话说,如果您有一个命令行项目和一个dll项目,并且该命令行项目正在执行new Foo(),但Foo是在dll项目中定义的,那么在其程序集信息中需要该行的正是dll项目。但是,如果在命令行项目中添加一个日志调用,该调用显示“我将调用Foo”,那么日志记录将停止工作(因为现在console项目是第一个进行日志调用的项目,配置行需要位于该程序集信息中!)


因此,最安全、最省事的方法是将该行添加到您所拥有的每个程序集信息中。

我假设您有一个控制台程序(看起来您正在测试log4net)。因此,我要做的第一件事是确保日志记录与更基本的appender一起工作。配置ConsolePender并确保看到日志语句。严格来说,这并不是必须的,但正如我所说,这就是我要做的

更重要的事情是打开它,它应该告诉您为什么没有写入事件日志


您很可能面临权限问题。Cf.log4net.

我没有提到的一件事是log4net.config文件是否正在复制到您的bin目录。我已经遇到过几次了,它很容易修复

在解决方案资源管理器中,右键单击Log4Net.config文件,并确保
复制到输出目录
的字段设置为
始终复制
复制(如果较新)
(您的首选项)。在第一次设置解决方案时,我倾向于忽略这一步


希望这能帮到你。

还是没什么。EventLog没有新条目,并且logger的所有参数都设置为falsephenevo配置的带有属性的log4net。无需调用此方法我删除了XmlConfigurator.Configure()并添加了此部分,但仍然相同:/log4net的configSections元素只有在app.config文件中有log4net配置时才是必需的。很抱歉,没有找到。但是,我的其余答案仍然适用。当您将log4net配置放在app.config文件中时,您需要将configSections元素放在配置文件的顶部Yes!这节省了我很多时间。。。