所以我开始这个新项目,并希望使用Log4net登录数据库。我正在使用EF进行其他事情并且连接正常。出于某种原因,L4N没有记录任何内容,但没有抛出任何错误。只是想知道我可能缺少哪些步骤,希望有人可以指出。我需要以某种方式“保存”到数据库吗?提前做了很多。
我的web.config:
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.EntityClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=myserver;initial catalog=Reporting;integrated security=false;persist security info=True;User ID=myid;Password=mypassword" />
<commandText value="INSERT INTO [myschema].[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="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="Info" />
<appender-ref ref="LogFileAppender" />
</root>
和我的控制员:
...
using log4net;
using System.Reflection;
namespace MyNames.Controllers
{
public class ClientController : Controller
{
ReportingEntities db = new ReportingEntities();
private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public ActionResult Index()
{
this.log.Debug("Sample message to log to Db");
return View();
}
}
}
编辑:所以我提出了mods建议gorilly,但仍然无法正常工作。刚碰巧检查输出窗口,即使我启用了调试它仍然显示这个
。。
尝试了here的调试建议,但没有抛出任何错误。
从我的log4net内部调试文件:
log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] Loaded From [C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\bf082a83\4eb20e9a\assembly\dl3\c62a3103\e01aa9f6_fda1cf01\DotNetOpenAuth.Core.dll]
log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] does not have a RepositoryAttribute specified.
log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] 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: Shutdown called on Hierarchy [log4net-default-repository]
答案 0 :(得分:6)
将此行添加到Global.asax.cs(在Application_Start()中):
log4net.Config.XmlConfigurator.Configure();
此外,您可以打开log4net调试,通过在<appSettings>
下的Web.config中添加此行来帮助您弄清楚出现了什么问题:
修改:以下内容为value="false"
,但应为value="true"
。
<add key="log4net.Internal.Debug" value="true"/>
修改强>
您可以通过将以下内容添加到Web.config中来将log4net的内部调试日志记录发送到文件中(请参阅官方说明here):
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\mylogdirectory\log4net_debug.txt" />
</listeners>
</trace>
</system.diagnostics>