1. 添加Log4net引用

2. 添加配置文件

Log.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<logger name="Log4NetTest">
<level value="ERROR"/>
<appender-ref ref="Log4NetTestAppender" />
<!--Log4NetTestAppender和下面进行对应-->
</logger> <logger name="RYJ.Crm.Quartz">
<level value="ERROR"/>
<appender-ref ref="QuartzAppender" />
</logger> <logger name="RYJ.Crm.Services">
<level value="ERROR"/>
<appender-ref ref="ServiceAppender" />
</logger> <logger name="RYJ.Crm.ExternalService">
<level value="ERROR"/>
<appender-ref ref="ExternalServiceAppender" />
</logger> <logger name="RYJ.Crm.HeartBeat">
<level value="DEBUG"/>
<appender-ref ref="HeartBeatAppender" />
</logger> <logger name="RYJ.Crm.System">
<level value="ERROR"/>
<appender-ref ref="SystemAppender" />
</logger> <logger name="RYJ.Crm.UrlFilter">
<level value="DEBUG"/>
<appender-ref ref="UrlFilterAppender" />
</logger> <logger name="NHibernate">
<level value="OFF"/>
<appender-ref ref="NHibernateAppender" />
</logger> <logger name="RYJ.Return">
<level value="DEBUG"/>
<appender-ref ref="ReturnAppender" />
</logger> <logger name="RYJ.Crm.Event">
<level value="DEBUG"/>
<appender-ref ref="EventAppender" />
</logger> <logger name="RYJ.Crm">
<level value="ERROR"/>
<appender-ref ref="EmailAppender" />
</logger> <!--返回值异常日志记录-->
<appender name="Log4NetTestAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/Log4NetTest/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--返回值异常日志记录-->
<appender name="ReturnAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/Return/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--事件异常日志记录-->
<appender name="EventAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/Event/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--系统异常日志记录-->
<appender name="SystemAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/System/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--调度日志记录-->
<appender name="QuartzAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/Quartz/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--Service日志记录-->
<appender name="ServiceAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/Service/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--调用外部服务日志记录-->
<appender name="ExternalServiceAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/ExternalService/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--HeartBeat日志记录-->
<appender name="HeartBeatAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/HeartBeat/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--UrlFilter日志记录-->
<appender name="UrlFilterAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/UrlFilter/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--NHibernate日志记录-->
<appender name="NHibernateAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/NHibernate/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender> <!--邮箱设置-->
<appender name="EmailAppender" type="log4net.Appender.SmtpAppender">
<!-- SMTP 验证方式 -->
<authentication value="Basic" />
<to value="" />
<from value="service@daishu360.com" />
<username value="service@daishu360.com" />
<password value="DSpuhui360.cn" />
<subject value="【本地环境】CRM后台API错误异常" />
<!--邮件服务端-->
<smtpHost value="smtp.exmail.qq.com"/>
<bufferSize value="" />
<lossy value="true" />
<!--这里我设置的是Error ,会接收(FATAL和ERROR日志)你可以根据自己的需要设置Log4net支持多种级别的日志。优先级从高到低依次排列如下:FATAL > ERROR > WARN > INFO > DEBUG-->
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="OFF"/>
</evaluator>
<!--输出格式-->
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
</log4net>
</configuration>

Web.config

 <!--log4net日志配置文件-->
<add key="log4net" value="config/log4net/log.config" />

3. 注册lognet

  protected void Application_Start()
{ //注册lognet
var log4NetFileName = FilePath(ConfigurationManager.AppSettings["log4net"]);
if (File.Exists(log4NetFileName))
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetFileName));
}
}
private static string FilePath(string name)
{
if (string.IsNullOrEmpty(name))
{
return string.Empty;
} return string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\', '/'), name.TrimStart('\\', '/'));
}

4. 编写Helper

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Log4NetTest
{
public static class Log4NetUtility
{
/// <summary>
/// 写日志
/// </summary> public static void WriteLog(this string message, string name, Log4NetType logType = Log4NetType.Info)
{
var log = GetLog(name);
if (log == null)
{
return;
} if (logType == Log4NetType.Debug)
{
log.Debug(message);
return;
} if (logType == Log4NetType.Error)
{
log.Error(message);
return;
} if (logType == Log4NetType.Fatal)
{
log.Debug(message);
return;
} if (logType == Log4NetType.Info)
{
log.Info(message);
return;
} log.Warn(message);
} /// <summary>
/// 写异常日志
/// </summary>
public static void WriteLog(this Exception exception, string name)
{
var log = GetLog(name);
if (log == null)
{
return;
} WriteLog(exception, log);
} /// <summary>
/// 写异常日志
/// </summary>
private static void WriteLog(Exception exception, ILog log)
{
if (exception == null)
{
return;
} log.Error(exception); WriteLog(exception.InnerException, log);
} /// <summary>
/// 获取日志对象
/// </summary>
public static ILog GetLog(string name)
{
var logger = LogManager.GetLogger(name);
return logger;
}
} /// <summary>
/// Log4Net类型枚举
/// </summary>
public enum Log4NetType
{
/// <summary>
/// 字段Debug
/// </summary> Debug, /// <summary>
/// 字段Error
/// </summary>
Error, /// <summary>
/// 字段Info
/// </summary>
Info, /// <summary>
/// 字段Fatal
/// </summary>
Fatal, /// <summary>
/// 字段Warn
/// </summary>
Warn
} /// <summary>
/// 类名称:Log4NetName
/// 命名空间:DaiShu.Crm.Utility
/// 类功能:日志名称
/// </summary>
public class Log4NetName
{
/// <summary>
/// 字段UrlFilter
/// </summary>
public const string Log4NetTest = "Log4NetTest"; /// <summary>
/// 字段UrlFilter
/// </summary>
public const string UrlFilter = "RYJ.Crm.UrlFilter"; /// <summary>
/// 字段System
/// </summary>
public const string System = "RYJ.Crm.System"; /// <summary>
/// 字段Quartz
/// </summary>
public const string Quartz = "RYJ.Crm.Quartz"; /// <summary>
/// 字段Service
/// </summary>
public const string Service = "RYJ.Crm.Services"; /// <summary>
/// 字段ExternalService
/// </summary>
public const string ExternalService = "RYJ.Crm.ExternalService"; /// <summary>
/// 字段Return
/// </summary>
public const string Return = "RYJ.Return"; /// <summary>
/// 字段HeartBeat
/// </summary>
public const string HeartBeat = "RYJ.Crm.HeartBeat"; /// <summary>
/// 字段Event
/// </summary>
public const string Event = "RYJ.Crm.Event";
}
}

调用演示

            try
{
int n=int.Parse("aaa");
}
catch (Exception ex)
{
ex.WriteLog(Log4NetName.Log4NetTest);
}

下载源码

日志记录~log4.net的更多相关文章

  1. .net错误日志记录(log4)

    Log4 web.config <!--这段放前面--> <configSections> <section name="log4net" type= ...

  2. log4net--不可多得的开源日志记录组件

    log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...

  3. SpringBoot之日志记录-专题四

    SpringBoot之日志记录-专题四 六.日志管理 6.1使用log4j记录日志 6.1.2新建log4j配置文件 文件名称log4j.properties #log4j.rootLogger=CO ...

  4. 基于AOP和ThreadLocal实现日志记录

    基于AOP和ThreadLocal实现的一个日志记录的例子 主要功能实现 : 在API每次被请求时,可以在整个方法调用链路中记录一条唯一的API请求日志,可以记录请求中绝大部分关键内容.并且可以自定义 ...

  5. 前端学HTTP之日志记录

    前面的话 几乎所有的服务器和代理都会记录下它们所处理的HTTP事务摘要.这么做出于一系列的原因:跟踪使用情况.安全性.计费.错误检测等等.本文将谥介绍日志记录 记录内容 大多数情况下,日志的记录出于两 ...

  6. RMS Server打开或关闭日志记录

    原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...

  7. 从零开始编写自己的C#框架(20)——框架异常处理及日志记录

    最近很忙,杂事也多,所以开发本框架也是断断续续的,终于在前两天将前面设定的功能都基本完成了,剩下一些小功能遗漏的以后发现再补上.接下来的章节主要都是讲解在本框架的基础上进行开发的小巧. 本框架主要有四 ...

  8. PHP日志记录规范PSR-3

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  9. 利用AOP与ToStringBuilder简化日志记录

    刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...

随机推荐

  1. 深度讲解智能硬件手机APP开发流程

    常州做APP开发公司紫竹云科技分析,智能硬件产品的软件开发,除了APP和后台之外还有一个固件端的开发,由于固件是要运行产品上的,不过此时的硬件也是刚开始进行研发,所以是无法提供硬件来运行固件的.因此在 ...

  2. [ Linux ] [ OS ] [ memory ] Linux 如何查看系統硬體的記憶體(RAM)資訊

    cat /proc/meminfo https://blog.longwin.com.tw/2013/05/linux-ram-memory-info-2013/

  3. MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache

    1.在Unbutu上安装memcached服务器 sudo apt-get update sudo apt-get install memcached 2. 确认memcached是否安装 要确认me ...

  4. Functor、Applicative 和 Monad(重要)

    Functor.Applicative 和 Monad Posted by 雷纯锋Nov 8th, 2015 10:53 am Functor.Applicative 和 Monad 是函数式编程语言 ...

  5. socket 的通信过程

    1.建立套接字 Linux在利用socket()系统调用建立新的套接字时,需要传递套接字的地址族标识符.套接字类型以及协议,其函数定义于net/socket.c中: asmlinkage long s ...

  6. jsoup解析页面

    package com.java.jsoup; /** * jsoup解析网页 * @author nidegui * @version 2019年4月29日 下午5:12:02 * */ impor ...

  7. BZOJ 1305: [CQOI2009]dance跳舞 网络最大流_二分答案_建模

    Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...

  8. jmeter实现多并发

    1.jmeter实现多并发 线程组:负载发生器,用以多线程或多进程的方式来模拟用户的使用行为.jmeter是以线程的方式来进行模拟用户的并发访问的

  9. Spring Boot浅谈(是什么/能干什么/优点和不足)

    1. Spring Boot是什么,解决哪些问题 1) Spring Boot使编码变简单 2) Spring Boot使配置变简单 3) Spring Boot使部署变简单 4) Spring Bo ...

  10. 【CodeForces 574B】Bear and Three Musketeers

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举每一条边(x,y) 然后再枚举y的出度z 看看g[x][z]是否等于1(表示联通) 如果等于1就说明找到了一个三元环,则尝试用它们的出度和-6更 ...