我有log4net设置并配置为插入到sql server 2005表中。我的表叫做Log。当我调用log4net方法时,它不会将任何数据输入到sql server中的日志数据库中。我的客户端c#代码没有收到任何错误。我是否需要将用户添加到sql的日志表中?现在我正在使用Windows身份验证。
<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>
这是我的日志数据库的SQL代码:
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]
这是我用来调用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);
}
}
这是我的界面:
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);
/// <summary>
/// Warnings the specified message.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="innerException">The innerException.</param>
void warning(string message, Exception innerException);
}
答案 0 :(得分:51)
<bufferSize value="100" />
据说它会在内存中保留100个日志,直到写入数据库。也许这就是为什么你在DB中看不到任何东西?
答案 1 :(得分:10)
仅仅通过查看您的配置我对您的问题没有任何见解,但您可以尝试将其添加到您的应用设置中,以查看log4net是否可以告诉您出现了什么问题:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
有关如何捕获log4net内部日志记录的说明,请参阅标题为"How do I enable log4net internal debugging?"的部分。
答案 2 :(得分:1)
我遇到了同样的问题。就我而言,我不得不从插入中删除[Exception]和@exception的使用。
所以,我不得不改变我的日志。我基本上必须创建一个很好的异常转储并将其传递给message参数。
我希望这会有所帮助
答案 3 :(得分:0)
除非您在连接字符串中专门设置用户,否则Log4Net将尝试使用分配给该进程的任何用户帐户进行日志记录(如果是这种情况,则为ASP.NET工作进程)。如果您无法登录,Log4Net也不会抛出异常。您可以打开调试以查看问题所在。