您现在的位置是:网站首页> 编程资料编程资料

asp.net中Log4.net的工具类helper_实用技巧_

2023-05-24 461人已围观

简介 asp.net中Log4.net的工具类helper_实用技巧_

一、Config文件配置

二、LogHelper工具类代码

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace log4netDemo2 { public class LogHelper { public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");//这里的 loginfo 和 log4net.config 里的名字要一样 public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");//这里的 logerror 和 log4net.config 里的名字要一样 //INFO public static void WriteLog(string info) { if (loginfo.IsInfoEnabled) { loginfo.Info(info); } } //ERROR public static void WriteLog(string info, Exception ex) { if (logerror.IsErrorEnabled) { logerror.Error(info, ex); } } } }

三、测试代码

using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace log4netDemo2 { class Program { static void Main(string[] args) { try { string a = "FF"; LogHelper.WriteLog(a); int b = Convert.ToInt32(a); } catch (Exception ex) { LogHelper.WriteLog(ex.Message.ToString(), ex); } } } }

四、测试输出

loginfo

logerror

到此这篇关于Log4.net工具类helper的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

-六神源码网