简体   繁体   English

log4Net无法按预期记录到控制台

[英]log4Net not logging to console as expected

I have two separate xml files I use for configuring log4Net. 我有两个用于配置log4Net的单独的xml文件。 One is to log to a file, the other is to log just to the console. 一种是登录到文件,另一种是仅登录到控制台。 I control which file gets read in my Global.asax.cs file in the Application_Start() method: 我通过Application_Start()方法控制在Global.asax.cs文件中读取哪个文件:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    //Initialize Log4Net
    XmlConfigurator.Configure(new System.IO.FileInfo("C:\\temp2\\DataTables_Examples\\DataTables_Examples\\Console.config"));
    log.Info("Initializing Log4Net...");
    Debug.WriteLine("MvcApplication - Initializing Log4Net...");

}

When I run my webapp using the xml file configured to write to a file, it works perfectly fine giving me the following output: 当我使用配置为写入文件的xml文件运行我的webapp时,它可以很好地工作,并提供以下输出:

2014-10-01 13:34:56,596 [6] ERROR CSED.Database
Error Number: 0
Error Code: -2147467259
Message: Authentication to host 'localhost' for user 'webapp' using method 'mysql_native_password' failed with message: Access denied for user 'webapp'@'localhost' (using password: YES)
Source: MySql.Data
StackTrace:    at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool..ctor(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPoolManager.GetPool(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at CSED.Database.createMySqlConnection(String connectionString) in c:\DataTables\CSED\CSED\Database.cs:line 395

This is just fine. 很好 I've deliberately changed the password on the connection string in web.config so that this exception would be thrown. 我故意更改了web.config中连接字符串上的密码,以便引发此异常。 Then I tried to configure log4Net to log to the console and I expected the same output. 然后,我尝试将log4Net配置为登录到控制台,并希望获得相同的输出。 NOT! 不!

My configuration file for the console looks like this: 我的控制台配置文件如下所示:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>    
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <!-- ConsoleAppender uses PatternLayout -->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c %m%n" />        
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

Nothing unusual there. 那里没什么异常。

For comparison, this is the config for logging to a file. 为了进行比较,这是用于登录文件的配置。 This works: 这有效:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>    
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file value="C:\\temp2\\DataTables_Examples\\DataTables_Examples\\log.txt"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c %m%n" />        
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>

And yet when I run my webapp I get the following: 但是,当我运行Webapp时,我得到以下信息:

log4net: log4net assembly [log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ff9156a3\74c0a06a\assembly\dl3\c94489ac\1765076f_58c5cf01\log4net.dll]. (.NET Runtime [4.0.30319.18444] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [DataTables_Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [DataTables_Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ff9156a3\74c0a06a\assembly\dl3\898de284\55bca487_97ddcf01\DataTables_Examples.dll]
log4net: Assembly [DataTables_Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [DataTables_Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: configuring repository [log4net-default-repository] using file [C:\temp2\DataTables_Examples\DataTables_Examples\Console.config]
log4net: configuring repository [log4net-default-repository] using stream
log4net: loading XML configuration
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [DEBUG].
log4net: Logger [root] level set to [name="DEBUG",value=30000].
log4net: Loading Appender [ConsoleAppender] type: [log4net.Appender.ConsoleAppender]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%d [%t] %-5p %c %m%n]
log4net: Converter [d] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [t] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [c] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [n] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Created Appender [ConsoleAppender]
log4net: Adding appender named [ConsoleAppender] to logger [root].
log4net: Hierarchy Threshold []
MvcApplication - Initializing Log4Net...
log4net: Creating repository for assembly [CSED, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [CSED, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ff9156a3\74c0a06a\assembly\dl3\d3a2a365\45b5ee3b_90ddcf01\CSED.dll]
log4net: Assembly [CSED, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [CSED, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]
Database - connection string name: LocalMySqlServer
Database - connString: port=3306;server=localhost;user id=webapp;password=foobar;database=editor;pooling=True;minpoolsize=2;maxpoolsize=4
BasicSSPController - Processing...
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

The Console.config file is definitely being read in by log4Net. log4Net肯定正在读取Console.config文件。 So why isn't it printing out to console the same as it does when logging to a file? 那么,为什么它不打印出与登录文件时一样的控制台信息呢? The conversionPattern is the same for both. 两者的conversionPattern相同。 All I'm getting is the one line at the bottom of the console. 我得到的只是控制台底部的一行。 I'm missing something somewhere. 我在某处想念东西。

One of your assemblies seems to use log4net as well. 您的程序集之一似乎也使用log4net。 Maybe there's some conflict there. 也许那里有些冲突。

...
log4net: Creating repository for assembly [CSED...
...
log4net: Creating repository for assembly [DataTables_Examples...
...
log4net: repository [log4net-default-repository] already exists...
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM