2

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.

Alan
  • 822
  • 1
  • 16
  • 39
  • Stupid idea, but any chance you are launching your console app in a way that redirects stdout to another process? The ConsoleAppender is the only one affected? – samy Oct 01 '14 at 21:31

1 Answers1

0

One of your assemblies seems to use log4net as well. 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...
...
celper
  • 175
  • 10
  • I had created a dll project earlier which this webapp is referencing. The dll project is called CSED.dll. It too is using log4Net to capture exceptions, but I don't initialize log4Net there at all. Only in the web application do I do that. I don't really understand this "repository" stuff and why it is creating one for the CSED.dll and another for the web app. If there is only supposed to be one repository, then how do I fix this? – Alan Oct 01 '14 at 19:03
  • a) Can you post your working log4net section for comparison? b) Do you have log4net internal logging activated in your appsettings? (with something like: ) If so, you could turn that off. c) Is this the same problem?(http://stackoverflow.com/questions/22596406/exception-when-adding-log4net-config) – celper Oct 01 '14 at 19:46
  • Yes I did that that setting set to true. I set it to false and ran the webapp again. But it didn't change anything. – Alan Oct 01 '14 at 19:56
  • I added the config for logging to file as a comparison as requested. – Alan Oct 01 '14 at 20:06
  • I also tried adding the <startup> tag set to the end of my config file as noted in the post you referred to, but again, that had no affect. – Alan Oct 01 '14 at 20:16
  • Is there any difference when you configure log4net inside your web.config? (I tested both your log4net sections in a c# console application. Both worked fine.) – celper Oct 01 '14 at 21:06
  • I tried moving the config info into the web.config file, but same results. – Alan Oct 01 '14 at 21:31
  • What happens if you add the console appender to your working log4net configuration file? () – celper Oct 02 '14 at 05:43