winform集成log4net框架

说明:使用winform集成log4net框架操作采用代码方式加载log4net引擎。
1 用户可以从http://logging.apache.org/log4net/下载log4net的源代码。解压软件包后,在解压的src目录下将log4net.sln载入Visual Studio .NET,编译后可以得到log4net.dll。用户要在自己的程序里加入日志功能,只需将log4net.dll引入工程即可。
2 新建Project,名称为:MyLog4net,框架选择.NET Framework2.0
3 新建类Logger如下:

using System;
using System.Collections.Generic;
using System.Text;
using log4net;

namespace MyLog4net
{
public class Logger
{
#region
public static ILog init()
{
DateTime now = DateTime.Now;
int year = now.Year;
int month = now.Month;
int date = now.Day;
int hour = now.Hour;
int minute = now.Minute;
int second = now.Second;
int millisecond = now.Millisecond;
string format = year + "/" + month + "/" + date + " " + hour + ":" + minute + ":" + second + ":" + millisecond;
//string pattern = "["+format+"]%n MESSAGE:%message 日志级别:%-5level%n";
string pattern = "[" + format + "]%n %-5level:[%message]%n%n";
log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout(pattern);
log4net.Appender.FileAppender file = new log4net.Appender.FileAppender(pl, "D:/log.txt");
log4net.Config.BasicConfigurator.Configure(file);
return log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}

public static void Debug(object message, Exception exception)
{
ILog log = init();
log.Debug(message, exception);
}

public static void Debug(object message)
{
ILog log = init();
log.Debug(message);
}

public static void Error(object message, Exception exception)
{
ILog log = init();
log.Error(message, exception);
}

public static void Error(object message)
{
ILog log = init();
log.Error(message);
}

public static void Fatal(object message, Exception exception)
{
ILog log = init();
log.Fatal(message, exception);
}

public static void Fatal(object message)
{
ILog log = init();
log.Fatal(message);
}

public static void Info(object message, Exception exception)
{
ILog log = init();
log.Info(message, exception);
}

public static void Info(object message)
{
ILog log = init();
log.Info(message);
}

public static void Warn(object message, Exception exception)
{
ILog log = init();
log.Warn(message, exception);
}

public static void Warn(object message)
{
ILog log = init();
log.Warn(message);
}

#endregion
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值