开发者

Log4net not inserting into the database?

I have log4net setup and configured to insert into a sql server 2005 table. My table is called Log. When I call the log4net method it does not enter any data into the log database in sql server. I am not getting any errors from my client c# code. Do I need to add a user to the log table in sql? Right now I am using windows authentication.

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
 <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
   <bufferSize value="100" />
   <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
connectionString value="Data Source=V-FIN-SQL-D\SQL2005;Initial Catalog=DevMHAIC;Integrated Security=True;Connection Timeout=360" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES
       (@log_date, @thread, @log_level, @logger, @message, @exception)" />
 <parameter>
   <parameterName value="@log_date" />
        <dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
   <parameter>
 <parameterName value="@thread" />
     <dbType value="String" />
   <size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
 </layout>
 </parameter>
   <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%p" />
    </layout>
  </parameter>
   <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%c" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%m" />
    </layout>
  </parameter>
   <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ADONetAppender" />
</root>
</log4net>

Here is my sql code for the log database:

CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NULL,
[Thread] [varchar](255) NULL,
[Level] [varchar](50) NULL,
[Logger] [varchar](255) NULL,
[Message] [varchar](4000) NULL,
[Exception] [varchar](2000) NULL
) ON [PRIMARY] 

Here is my class that I am using to call the log4net.

public class ErrorLog : ILog
{
    log4net.ILog _Log;

    /// <summary>
    /// Initializes a new instance of the <see cref="ErrorLog"/> class.
    /// </summary>
    /// <param name="log">The log.</param>
    public ErrorLog(log4net.ILog log) : this()
    {
        _Log = log;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ErrorLog"/> class.
    /// </summary>
    public ErrorLog()
    {
        _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        log4net.Config.XmlConfigurator.Configure(); 
    }

    /// <summary>
    /// Informationals the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void informational(string message)
    {
        _Log.Info(message);
    }

    /// <summary>
    /// Informationals the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    public void informational(string message, Exception innerException)
    {
        _Log.Info(message, innerException);
    }

    /// <summary>
    /// Debugs the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void debug(string message)
    {
        _Log.Debug(message);
    }

    /// <summary>
    /// Debugs the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    public void debug(string message, Exception innerException)
    {
        _Log.Debug(message, innerException);
    }

    /// <summary>
    /// Errors the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void error(string message)
    {
        _Log.Error(message);
    }

    /// <summary>
    /// Errors the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    public void error(string message, Exception innerException)
    {
        _Log.Error(message, innerException);
    }

    /// <summary>
    /// Warnings the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void warning(string message)
    {
        _Log.Warn(message);
    }

    /// <summary>
    /// Warnings the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    public void warning(string message, Exception innerException)
    {
        _Log.Warn(message, innerException);
    }
}

Here is my interface:

public interface ILog
{
    /// <summary>
    /// Informationals the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    void informational(string message);

    /// <summary>
    /// Informationals the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    void informational(string message, Exception innerException);

    /// <summary>
    /// Debugs the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    void debug(string message);

    /// <summary>
    /// Debugs the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    void debug(string message, Exception innerException);

    /// <summary>
    /// Errors the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    void error(string message);

    /// <summary>
    /// Errors the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    void error(string message, Exception innerException);

    /// <summary>
    /// Warnings the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    void warning(string message);

  开发者_Go百科  /// <summary>
    /// Warnings the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="innerException">The innerException.</param>
    void warning(string message, Exception innerException);
}


<bufferSize value="100" />

It is saying that it will keep 100 logs in memory until written into DB. Maybe that's why you don't see anything in DB?


I don't really have any insight to your problem just by looking at your configuration, but you might try adding this to your app settings to see if log4net can tell you what is going wrong:

<appSettings>         
  <add key="log4net.Internal.Debug" value="true"/> 
</appSettings> 

See the section titled "How do I enable log4net internal debugging?" for a description of how to capture log4net's internal logging.


I had the same problem. In my case, I had to remove the use of [Exception] and @exception from the insert.

So, I had to change my logging too. I basically had to create a nice dump of my exception and pass that into the message parameter.

I hope this helps


Unless you specifically set a user in your connection string Log4Net will attempt to log with whatever user account is assigned to the process (ASP.NET worker process if that is the case). Log4Net also will not throw exceptions if you it is unable to log. You can turn on debugging to see what the issue is.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜