开发者

Log4Net Issue with .Net 4.0

开发者 https://www.devze.com 2023-03-14 13:37 出处:网络
I have looked for a \"fix\" to my issue for a few days not and have gotten nowhere. Basically what is happening is that I have an application that writes to a database and sends an email via log4Net.

I have looked for a "fix" to my issue for a few days not and have gotten nowhere. Basically what is happening is that I have an application that writes to a database and sends an email via log4Net.

Here is where I initialize my log object:

private static readonly log4net.ILog log;
#endregion

static PTSentryService()
{
    // Initialize log4net.
    log4net.Config.XmlConfigurator.Configure();
    log = log4net.LogManager.GetLogger(typeof(PTSentryService));        
}

I then use the LogHelper class to grab the configuration information, below is what I have in my Web.Config:

<log4net>
<appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="Data Source=ServerName;Initial Catalog=EventLog;Trusted_Connection=no;User ID=EventLogAdmin;Password=EVLA%dtm" />
  <commandText value="INSERT INTO EventLog ([TimeStamp],[SeverityLevel],[LoggerName],[Environment],[ApplicationName],[ApplicationPath],[ServerName],[Message],[ExceptionData]) VALUES (@TimeStamp,@SeverityLevel,@LoggerName,@Environment,@ApplicationName,@ApplicationPath,@ServerName,@Message,@ExceptionData)" />
  <parameter>
    <parameterName value="@TimeStamp" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@SeverityLevel" />
    <dbType value="String" />
    <size value="10" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@LoggerName" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%logger" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@Environment" />
    <dbType value="String" />
    <size value="4" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{EnvironmentName}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@ApplicationName" />
    <dbType value="String" />
    <size value="128" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{AppName}" />开发者_开发知识库;
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@ApplicationPath" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{AppPath}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@ServerName" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{log4net:HostName}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@Message" />
    <dbType value="String" />
    <size value="-1" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@ExceptionData" />
    <dbType value="String" />
    <size value="-1" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{exceptionDetails}" />
    </layout>
  </parameter>
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="Mailbox@group.com" />
  <from value="Mailbox@group.com" />
  <subject value="(LOCL) PT Sentry" />
  <smtpHost value="group.com" />
  <bufferSize value="1" />
  <lossy value="false" />
  <threshold value="ERROR" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate [%thread] %-5level %logger [%property{log4net:HostName}] - %message%newline%newlineException details:%newline%property{exceptionDetails}" />
  </layout>
</appender>

<root>
  <level value="INFO" />
  <appender-ref ref="ADONetAppender_SqlServer" />
  <appender-ref ref="SmtpAppender" />
</root>

What occurs is that all the properties in the ILog object (isErrorEnabled, etc) are all false. I know this is due to the config not being loaded properly, but this is a copy paste job from a .Net 3.5 project that I know works. Are there changes that I need to make to accommodate 4.0?

Any info would be excellent, thanks!


What you may want to do is try simplifying your config to see if it's a certain appender or other config value that is causing this. Maybe take out your database appender and see if it works.

Also I encountered this issue before when my references folder was jacked up. You can check the SO article out here: log4net not working in dll

One other thing that I usually see is the logger actually being declared before the call to configure. I doubt that this matters but just throwing stuff out there.

private static readonly log4net.ILog log = log = log4net.LogManager.GetLogger(typeof(PTSentryService));

static PTSentryService()
{
    // Initialize log4net.
    log4net.Config.XmlConfigurator.Configure();
}

Edit:

In addition to this you may want to turn on log4net logging to see if you see anything fishy.

0

精彩评论

暂无评论...
验证码 换一张
取 消