ASP.NET/MVC 配置log4net启用写错误日志功能

2024-01-11 15:32

本文主要是介绍ASP.NET/MVC 配置log4net启用写错误日志功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<?xml version="1.0" encoding="utf-8"?>
<!--有关如何配置 ASP.NET 应用程序的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=169433-->
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /><!--①配置log4net开始 使用nuget控制台 Install-Package log4net --><!-- 在AssemblyInfo.cs加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", ConfigFileExtension = "config", Watch = true)]--><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/><!--①配置log4net结束--></configSections><!--②配置log4net开始--><log4net><logger name="logerror"><level value="ALL" /><appender-ref ref="ErrorAppender" /></logger><logger name="loginfo"><level value="ALL" /><appender-ref ref="InfoAppender" /></logger><appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Log\\LogError\\" /><param name="AppendToFile" value="true" /><param name="MaxSizeRollBackups" value="100" /><param name="MaxFileSize" value="10240" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd'.txt'" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><param name="ConversionPattern" value="%d [%t] [%-5level] : %message %newline"  /></layout></appender><appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Log\\LogInfo\\" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd'.txt'" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><param name="ConversionPattern" value="%d [%t] [%-5level] : %message %newline"  /></layout></appender></log4net><!--②配置log4net结束--><connectionStrings><add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WWW-20160719034030.mdf;Initial Catalog=aspnet-WWW-20160719034030;Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings><system.web><authentication mode="None" /><compilation debug="true" targetFramework="4.5.2" /><httpRuntime targetFramework="4.5.2" /><pages><namespaces><add namespace="System.Web.Optimization" /><add namespace="Microsoft.AspNet.Identity" /></namespaces><controls><add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /></controls></pages><membership><providers><!--已在此模板中禁用 ASP.NET 成员身份。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持--><clear /></providers></membership><profile><providers><!--已在此模板中禁用 ASP.NET 成员身份配置文件。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持--><clear /></providers></profile><roleManager><!--已在此模板中禁用 ASP.NET 成员身份角色。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持--><providers><clear /></providers></roleManager><!--如果要部署到具有多个 Web 服务器实例的云环境,则应将会话状态模式从 "InProc" 更改为“自定义”。此外,还应将名为 "DefaultConnection" 的连接字符串更改为连接到SQL Server (包括 SQL Azure 和 SQL  Compact)实例,而不是连接到 SQL Server Express 实例。--><sessionState mode="InProc" customProvider="DefaultSessionProvider"><providers><add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /></providers></sessionState></system.web><system.webServer><modules><remove name="FormsAuthentication" /></modules></system.webServer><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /></dependentAssembly><dependentAssembly><assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" /><bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /></dependentAssembly></assemblyBinding></runtime><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="mssqllocaldb" /></parameters></defaultConnectionFactory><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /></providers></entityFramework><system.codedom><compilers><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" /><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+" /></compilers></system.codedom>
</configuration>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;public class LogHelper
{public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");public static void WriteInfo(string info){loginfo.Info(info);}public static void WriteError(string info, Exception ex){logerror.Error(info, ex);}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WWW
{public partial class _Default : Page{protected void Page_Load(object sender, EventArgs e){LogHelper.WriteInfo(string.Format("当前的时间{0}", DateTime.Now.ToString()));try{Convert.ToInt32("aaa");}catch (Exception ex){LogHelper.WriteError(string.Format("当前的时间{0}", DateTime.Now.ToString()), ex);}}}
}

这篇关于ASP.NET/MVC 配置log4net启用写错误日志功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/594835

相关文章

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构