.Net MVC下 log4net配置
第一步:引入第三方类库
链接:https://pan.baidu.com/s/1mP9ywAxjEQOf4Q6RP2OGOQ
提取码:j2e2
第二步:web.config配置
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 For more information on how to configure your ASP.NET application, please visit 4 http://go.microsoft.com/fwlink/?LinkId=301880 5 --> 6 <configuration> 7 <configSections> 8 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 9 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 10 11 <!--Log4net块配置--> 12 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 13 </configSections> 14 15 <connectionStrings> 16 <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Sam.OA.WEBAPP-20190801094316.mdf;Initial Catalog=aspnet-Sam.OA.WEBAPP-20190801094316;Integrated Security=True" providerName="System.Data.SqlClient" /> 17 </connectionStrings> 18 <appSettings> 19 <!--数据库配置--> 20 <add key="ConnectionString" value="server=.;uid=sa;pwd=0;database=Sam.OA.DB" /> 21 <add key="dbType" value="sqlserver" /> 22 <add key="PrintErrorSqlPath" value="D://NewError.txt" /> 23 <add key="IsPrint" value="false" /> 24 <!--数据库配置--> 25 <!--分布式缓存,服务器IP和端口请用冒号隔开,若有多台服务器请用“,”隔开,例如: <add key="MemcacheServiceList" value="172.20.10.7:11211,172.20.10.8:11211"/>--> 26 <add key="MemcacheServiceList" value="127.0.0.1:11211"/> 27 <!--分布式缓存--> 28 <add key="webpages:Version" value="3.0.0.0" /> 29 <add key="webpages:Enabled" value="false" /> 30 <add key="ClientValidationEnabled" value="true" /> 31 <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 32 </appSettings> 33 <system.web> 34 <authentication mode="None" /> 35 <compilation debug="true" targetFramework="4.5.2" /> 36 <httpRuntime targetFramework="4.5.2" /> 37 <httpModules> 38 <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 39 </httpModules> 40 </system.web> 41 <system.webServer> 42 <modules> 43 <remove name="FormsAuthentication" /> 44 <remove name="ApplicationInsightsWebTracking" /> 45 <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 46 </modules> 47 <validation validateIntegratedModeConfiguration="false" /> 48 </system.webServer> 49 <runtime> 50 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 51 <dependentAssembly> 52 <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 53 <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 54 </dependentAssembly> 55 <dependentAssembly> 56 <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 57 <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 58 </dependentAssembly> 59 <dependentAssembly> 60 <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 61 <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 62 </dependentAssembly> 63 <dependentAssembly> 64 <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 65 <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 66 </dependentAssembly> 67 <dependentAssembly> 68 <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 69 <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 70 </dependentAssembly> 71 <dependentAssembly> 72 <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 73 <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 74 </dependentAssembly> 75 <dependentAssembly> 76 <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 77 <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 78 </dependentAssembly> 79 <dependentAssembly> 80 <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 81 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 82 </dependentAssembly> 83 <dependentAssembly> 84 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 85 <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 86 </dependentAssembly> 87 <dependentAssembly> 88 <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 89 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 90 </dependentAssembly> 91 </assemblyBinding> 92 </runtime> 93 <entityFramework> 94 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 95 <parameters> 96 <parameter value="mssqllocaldb" /> 97 </parameters> 98 </defaultConnectionFactory> 99 <providers> 100 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 101 </providers> 102 </entityFramework> 103 <system.codedom> 104 <compilers> 105 <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 106 <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> 107 </compilers> 108 </system.codedom> 109 110 <log4net> 111 <!--OFF,FATAL,ERROR,WARN,INFO,DEBUG,ALL--> 112 <!--Set root logger level to ERROR and its appenders--> 113 <root> 114 <level value="ERROR" /> 115 <appender-ref ref="SysAppender" /> 116 </root> 117 118 <!--Print only messages of level DEBUG or above in the packages--> 119 <logger name="WebLogger"> 120 <level value="DEBUG" /> 121 </logger> 122 123 <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> 124 <param name="File" value="LogFile/" /> 125 <param name="AppendToFile" value="true" /> 126 <param name="RollingStyle" value="Date" /> 127 <param name="DatePattern" value="yyyyMMdd".txt"" /> 128 <param name="StaticLogFileName" value="false" /> 129 <param name="MaxSizeRollBackups" value="100" /> 130 <param name="MaxFileSize" value="10240" /> 131 <layout type="log4net.Layout.PatternLayout,log4net"> 132 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> 133 </layout> 134 <!--< > = <> %n = 回车--> 135 </appender> 136 <appender name="consoleApp" type="log4net.Appender.RollingFileAppender,log4net"> 137 <layout type="log4net.Layout.PatternLayout"> 138 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> 139 </layout> 140 </appender> 141 </log4net> 142 </configuration>
第三步:Global.asax的Application_Start()下初始化对象
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Optimization; 7 using System.Web.Routing; 8 9 namespace Sam.OA.WEBAPP 10 { 11 public class MvcApplication : System.Web.HttpApplication 12 { 13 protected void Application_Start() 14 { 15 AreaRegistration.RegisterAllAreas(); 16 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 17 RouteConfig.RegisterRoutes(RouteTable.Routes); 18 BundleConfig.RegisterBundles(BundleTable.Bundles); 19 //从配置文件读取log4net的配置,然后进行一个初始化工作。 20 log4net.Config.XmlConfigurator.Configure(); 21 } 22 } 23 }
第四步:重写过滤器RegisterGlobalFilters(GlobalFilterCollection filters)
1 using Sam.OA.WEBAPP.Models; 2 using System.Web.Mvc; 3 4 namespace Sam.OA.WEBAPP 5 { 6 public class FilterConfig 7 { 8 public static void RegisterGlobalFilters(GlobalFilterCollection filters) 9 { 10 //filters.Add(new HandleErrorAttribute()); 11 filters.Add(new MyExceptionFilterAttribute()); 12 } 13 } 14 }
MyExceptionFilterAttribute.cs
1 using Sam.OA.Common; 2 using System.Web.Mvc; 3 4 namespace Sam.OA.WEBAPP.Models 5 { 6 public class MyExceptionFilterAttribute: HandleErrorAttribute 7 { 8 public override void OnException(ExceptionContext filterContext) 9 { 10 base.OnException(filterContext); 11 LogHelper.WriteLog(filterContext.Exception.ToString()); 12 } 13 } 14 }

浙公网安备 33010602011771号