C#很多异步机制使程序无法使用断点调试,这时候我们就需要使用日志输出。使用log4net一定要先引用net4log这个dll,不然无法使用。

作者很懒,直接上代码吧。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net; namespace HacFin.SuperCapture.Switcher
{
public static class LogHelper
{
private static bool IsLogOpen = true;
public static ILog log = log4net.LogManager.GetLogger("HKFY_");
public static void LogUserAction(this Object sender, String msg)
{
if (!IsLogOpen) return;
LogDebug(sender, msg, "UserAction");
} public static void LogMeasureIn(this Object sender, String msg)
{
if (!IsLogOpen) return;
LogInfo(sender, msg, "Measure_In");
} public static void LogMeasureOut(this Object sender, String msg)
{
if (!IsLogOpen) return;
LogInfo(sender, msg, "Measure_Out");
} public static void LogDebug(this Object sender, String msg = "", String title = "")
{
if (!IsLogOpen) return;
String txt = title + " - " + msg;
log.Debug(txt);
} public static void LogWarn(this Object sender, String msg = "", String title = "")
{
if (!IsLogOpen) return;
String txt = title + " - " + msg;
log.Warn(txt);
} #region - 致命错误不受开关控制,一直需要记录
public static void LogError(this Object sender, String msg = "", String title = "")
{
String txt = title + " - " + msg;
log.Error(txt);
} public static void LogFatal(this Object sender, String msg = "", String title = "")
{
String txt = title + " - " + msg;
log.Fatal(txt);
}
#endregion
public static void LogInfo(this Object sender, String msg = "", String title = "")
{
if (!IsLogOpen) return;
String txt = title + " - " + msg;
log.Info(txt);
} public static String ToFullLogInfo(this Exception ex)
{
if (ex == null) return String.Empty; String exMsg = ex.Message;
if (ex.Source != null) exMsg += Environment.NewLine + ex.Source;
if (ex.StackTrace != null) exMsg += Environment.NewLine + ex.StackTrace;
if (ex.InnerException != null)
{
exMsg += Environment.NewLine + "InnerException : " + ex.InnerException.Message;
if (ex.InnerException.Source != null) exMsg += Environment.NewLine + ex.InnerException.Source;
if (ex.InnerException.StackTrace != null) exMsg += Environment.NewLine + ex.InnerException.StackTrace;
}
return exMsg;
} }
}

C#使用log4net的更多相关文章

  1. Log4net - 规则简介

    参考页面: http://www.yuanjiaocheng.net/CSharp/csharprumenshili.html http://www.yuanjiaocheng.net/entity/ ...

  2. Log4net - 项目使用的一个简单Demo

    参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entit ...

  3. log4net使用手册

    1. log4net简介 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.Java平台下,它还 ...

  4. Log4Net应用问题

    问题 一.日志存储方式 1.txt 2.SQLServer数据库 3.log文件 二.项目类型不同 1winFrom 2webFrom 3MVC 4WPF 5控制台 三.切分依据不同 1.空间大小 2 ...

  5. 在C#代码中应用Log4Net系列教程(附源代码)

    Log4Net应该可以说是DotNet中最流行的开源日志组件了.以前需要苦逼写的日志类,在Log4Net中简单地配置一下就搞定了.没用过Log4Net,真心不知道原来日志组件也可以做得这么灵活,当然这 ...

  6. Log4net入门(帮助类篇)

    在前几篇Log4net入门文件的讲述过程中,我们在使用log4net的类中都要编写如下一行代码: private static log4net.ILog log = log4net.LogManage ...

  7. Log4net入门(WCF篇)

    在上一篇Log4net入门(ASP.NET MVC 5篇)中,我们讲述了如何在ASP.NET MVC 5项目中使用log4net.在这一篇中,我们将讲述如何在WCF应用中使用log4net,为了讲述这 ...

  8. Log4net入门(ASP.NET MVC 5篇)

    在前4篇Log4net入门文章中,我们讲述了log4net的一些简单用法,在这一篇中我们主要讲述如何在ASP.NET MVC 5项目中将日志信息写入SQL Server数据库中. 一.创建最简单的AS ...

  9. Log4net入门(SQL篇)

    我们在Log4net入门(回滚日志篇)中详细讲述了如何将日志信息输出到日志文件中,在这一篇中,我们将讲述如何将日志文件写入SQL Server数据库,以方便我们分析统计日志信息. 首先,我们在SQL ...

  10. Log4net入门(回滚日志文件篇)

    在上一篇Log4net(日志文件篇)中,我们使用"log4net.Appender.FileAppender"将日志信息输出到一个单一的文件中,随着应用程序的持续使用,该日志文件会 ...

随机推荐

  1. python os.walk()遍历

    os.walk()遍历 import os p='/bin' #设定一个路径 for i in os.walk(p): #返回一个元组 print (i) # i[0]是路径 i[1]是文件夹 i[2 ...

  2. Xamarin.Forms DataGrid

    控件出处 https://components.xamarin.com/ https://components.xamarin.com/gettingstarted/ZumeroDataGrid/tr ...

  3. 【李婶小教程】(SE_lab3)

    额,今天说一下这个SE_lab3那几个东西都是怎么装的. 啊--其实技术含量貌似不是很高的. 1.先说最简单的一个叫做:Findbugs 点这个Eclipse Marketplace,这是一个神奇的东 ...

  4. HDU-2571命运

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了!可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个机 ...

  5. [饭后算法系列] "头尾移动" 排序列表

    1. 问题 一个乱序列表(list), 只支持两种操作: 把一个元素移动到头部, 或者把一个元素移动到尾部. 需要设计一种算法, 使得移动次数最少而使列表有序 举两个例子: 1. {3,5,7,1,9 ...

  6. POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据

    转自:http://blog.csdn.net/little_stars/article/details/8266262 前文讲述了 POI 读取的基本操作,但后期 经过试验,当写入数据量超过5万条以 ...

  7. 对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀

    功能简介 对象拷贝的应用现状简介: 业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最安全的做法,但是当数据对象的属性字段数量超过程序员的容忍的程度,代码因此变得臃肿不堪,使用 ...

  8. 把本地建好的项目提交到git上

    才开始用git来控制项目版本,刚开始时不是很会用,由于公司最近新开个项目,需要我把建好的项目放到git上去,慢慢的摸索,终于有点小小的结果,把一个项目成功提交到git上了,在这里记录下,以免下次忘记, ...

  9. 10个利用Eclipse调试Java的常见技巧

    http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...

  10. CSU1659: Graph Center(最短路)

    Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...