奇怪但我的MVC4应用程序没有将异常记录到数据库。当ExceptionHandlingAttribute出现异常时,应该创建日志条目,但不是。在VS的调试窗口或事件浏览器中没有显示log4not的错误。 Log4net配置由命令`log4net.Config.XmlConfigurator.Configure()运行;在Global.asax的方法Application_Start。
如何让log4net显示配置错误消息?
代码:
public class ExceptionHandlingAttribute : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MvcApplication));
logger.Error(filterContext.Exception.Message, filterContext.Exception);
Debug.WriteLine(string.Format("Exception: {0}", filterContext.Exception.Message));
}
}
配置:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<system.diagnostics />
<connectionStrings>
<!--
<add name="MembershipConnection" ....
</connectionStrings>
<appSettings>
<!-- app settings starts -->
...
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.web>
<customErrors mode="On" />
<globalization uiCulture="en-GB" culture="en-GB" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
...
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogIn" timeout="2880" />
</authentication>
<httpRuntime />
<profile defaultProvider="DefaultProfileProvider">
<providers>
...
</providers>
</sessionState>
<pages controlRenderingCompatibilityVersion="4.0">
<namespaces>
...
</namespaces>
</pages>
<!--
<siteMap defaultProvider="CustomSiteMapProvider" enabled="true">
<providers>
<clear/>
<add name="CustomSiteMapProvider" type="MvcWebRole1.Controllers.CustomSiteMapProvider"/>
</providers>
</siteMap>
-->
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
<!-- turn off gzipped response-->
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
</system.webServer>
<runtime>
...
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=.\SQLEXPRESS;initial catalog=devDB;integrated security=true;" />
<commandText value="INSERT INTO Logs ([Level], [Logger], [Message], [Exception], [AccountId], [TimeStamp]) VALUES (@log_level, @logger, @message, @exception, @account_id, @log_date)" />
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="30" />
<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="255" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter>
<parameterName value="@account_id" />
<dbType value="Int32" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
</configuration>
使用SELECT代码:
SELECT TOP 1000 [Id]
,[Level]
,[Logger]
,[Message]
,[Exception]
,[AccountId]
,[TimeStamp]
FROM [devDB].[dbo].[Logs]
更新1)
log4net重新安装到版本&#34; 2.0.2&#34; (nuget)=&gt; &#34; 1.2.12&#34;但仍然无法正常工作。
更新2) 我修复了配置文件,现在可以正常工作,但不完全:)。
当在本地启动应用程序并模拟异常时,不会在数据库中创建日志,而是保存在某处:)。当应用程序关闭时,SQL日志表中仍然没有数据。但是当再次启动应用程序时,日志会出现在sql表中。有什么问题:)?
解决: 我不得不将缓冲区大小从100更改为1 :),当缓冲区未满时,不保存数据:)
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
答案 0 :(得分:3)
您可以打开内部调试,看看额外信息是否告诉您问题所在。
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
请参阅http://logging.apache.org/log4net/release/faq.html和How to track down log4net problems