I have two separate xml files I use for configuring log4Net. 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:
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:
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. Then I tried to configure log4Net to log to the console and I expected the same output. 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:
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. 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. All I'm getting is the one line at the bottom of the console. I'm missing something somewhere.