WPF log4Helper

原创 2016年08月30日 17:23:47
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZYG.Alatin.Common
{
    /// /// <summary>
    /// 类说明:日志帮助类
    /// 创建人: pqw
    /// 创建时间: 2016-08-16
    /// 最后修改人
    /// 最后修改时间:
    public class LogHelper
    {
        private static log4net.ILog logger = null;//写日志实体
        /// <summary>
        /// 构造函数
        /// </summary>
        static LogHelper()
        {
            //Appender1 错误日志配置
            log4net.Filter.LevelRangeFilter levfilter = new log4net.Filter.LevelRangeFilter();
            levfilter.LevelMax = log4net.Core.Level.Fatal;
            levfilter.LevelMin = log4net.Core.Level.Error;
            levfilter.ActivateOptions();
            //Appender1 
            log4net.Appender.FileAppender appender1 = new log4net.Appender.FileAppender();
            appender1.AppendToFile = true;
            appender1.File = string.Format("{0}\\alatin\\runlog\\ErrorLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender1.ImmediateFlush = true;
            appender1.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender1.Name = "ErrorAppender";
            appender1.AddFilter(levfilter);
            //Appender2  调试日志配置
            log4net.Filter.LevelRangeFilter levfilter2 = new log4net.Filter.LevelRangeFilter();
            levfilter2.LevelMax = log4net.Core.Level.Debug;
            levfilter2.LevelMin = log4net.Core.Level.Debug;
            levfilter2.ActivateOptions();
            log4net.Appender.FileAppender appender2 = new log4net.Appender.FileAppender();
            appender2.AppendToFile = true;
            appender2.File = string.Format("{0}\\alatin\\runlog\\DebugLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender2.ImmediateFlush = true;
            appender2.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender2.Name = "DebugAppender";
            appender2.AddFilter(levfilter2);
            ///Appender3  所有日志配置
            log4net.Appender.FileAppender appender3 = new log4net.Appender.FileAppender();
            appender3.AppendToFile = true;
            appender3.File = string.Format("{0}\\alatin\\runlog\\AllLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender3.ImmediateFlush = true;
            appender3.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender3.Name = "AllAppender";
            ///layout 
            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("[%date [%thread] %-5level] %newline %message%newline%newline");
            layout.Header = "------ New session ------" + Environment.NewLine;
            layout.Footer = "------ End session ------" + Environment.NewLine;
            // 
            appender1.Layout = layout;
            appender2.Layout = layout;
            appender3.Layout = layout;
            appender1.ActivateOptions();
            appender2.ActivateOptions();
            appender3.ActivateOptions();
            // 
            log4net.Repository.ILoggerRepository repository = log4net.LogManager.CreateRepository("MyRepository");
            log4net.Config.BasicConfigurator.Configure(repository, appender1);
            log4net.Config.BasicConfigurator.Configure(repository, appender2);
            log4net.Config.BasicConfigurator.Configure(repository, appender3);
            logger = log4net.LogManager.GetLogger(repository.Name,"Admin");
            //logger.Fatal("Fatal");
        }
        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Debug(string message)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(message);
            }
        }
        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Debug(System.Exception ex)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(ex.Message.ToString() + "/r/n" + ex.Source.ToString() + "/r/n" + ex.TargetSite.ToString() + "/r/n" + ex.StackTrace.ToString());
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Error(string message)
        {
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Error(System.Exception ex)
        {
            if (logger.IsErrorEnabled)
            {
                if (ex.StackTrace != null)
                {
                    logger.Error(ex.Message.ToString() + "/r/n" + ex.Source.ToString() + "/r/n" + ex.TargetSite.ToString() + "/r/n" + ex.StackTrace.ToString());
                }
            }
        }
        /// <summary>
        /// 致命错误日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Fatal(string message)
        {
            if (logger.IsFatalEnabled)
            {
                logger.Fatal(message);
            }
        }
        /// <summary>
        /// 信息日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Info(string message)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info(message);
            }
        }
        /// <summary>
        /// 警告日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Warn(string message)
        {
            if (logger.IsWarnEnabled)
            {
                logger.Warn(message);
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

c# wpf log4net

  • 2014-10-24 12:35
  • 336KB
  • 下载

wpf log4net richtextbox

  • 2016-04-30 00:16
  • 127KB
  • 下载

LOG Helper

在终端打印LOG是最基础、最易用、某种程度上也是最经济的Debug方式。但是Base的规模大了,管理LOG也不是一件轻松的事情。 最近项目组在Linux内核里写一个新的mach,和该mach专用的驱...

log4net wpf 写的一个demo

  • 2016-01-20 09:59
  • 13.35MB
  • 下载

C# helper to dump any object to a log (zz)

<!-- span {font-family:'YaHei Consolas Hybrid'; font-size:10pt; color:#000000} .zide {colo...
  • is2120
  • is2120
  • 2012-08-27 11:03
  • 1020

IP修改Helper4

  • 2010-05-21 11:12
  • 271KB
  • 下载

基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之九:Hibernate Session Helper类,用于Entity属性与表字段的操作...

花了半小时写了一个helper,用于查找entity属性获得字段名,获得entity的字段列表,设置entity的属性值等,日后有需要再慢慢功能。 package com.freestyle.comm...

AspNet MVC4 教学-27:Asp.Net MVC4 自定义helper及function的快速Demo

Asp.Net MVC4 自定义helper及function的快速Demo

WPF 4 Ribbon 开发 之 快捷工具栏(Quick Access Toolbar)

在Office 2007 和Windows 7 两款产品中微软开始引入了一种新概念:“Ribbon 工具栏”,Ribbon 工具栏的界面设计模式可以使用户方便快捷的找到所需的工具,同时这种直观的设计形...

基于.NET WPF+ASP.NET MVC4技术构建夜猫商务会所运营管理平台

给你推荐一门由北风网闲争老师录制的《基于.NET WPF+ASP.NET MVC4技术构建夜猫商务会所运营管理平台一体化解决方案》 这是一门基于企业级项目而讲解的视频教程 咨询QQ:219...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)