首页 > Windows开发 > 详细

C#使用log4net

时间:2015-11-09 15:34:13      阅读:300      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/microstep/p/4949959.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!