我有两个单独的xml文件,用于配置log4Net。一种是登录到文件,另一种是仅登录到控制台。我控制在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...");
}
当我使用配置为写入文件的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
这很好。我故意更改web.config中连接字符串的密码,以便抛出此异常。然后我尝试配置log4Net以登录到控制台,我期望相同的输出。不是!
我的控制台配置文件如下所示:
<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>
那里没什么不寻常的。
为了进行比较,这是用于记录到文件的配置。这有效:
<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>
然而,当我运行我的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
log4Net肯定会读入Console.config文件。那么,为什么打印到控制台时没有像登录文件那样打印出来? convertPattern对于两者都是相同的。我得到的只是控制台底部的一行。我在某处遗漏了某些东西。
答案 0 :(得分:0)
你的一个程序集似乎也使用log4net。也许那里有一些冲突。
...
log4net: Creating repository for assembly [CSED...
...
log4net: Creating repository for assembly [DataTables_Examples...
...
log4net: repository [log4net-default-repository] already exists...
...