log4配置

netCore log4配置

 1 /// <summary>
 2     /// 使用LOG4NET记录日志的功能,在WEB.CONFIG里要配置相应的节点
 3     /// </summary>
 4     public class LogHelper
 5     {
 6         //log4net日志专用
 7 
 8         private static ILoggerRepository repository = LogManager.CreateRepository("AgentRepository");
 9 
10         private static ILog Loginfo;
11         private static ILog Logerror;
12         public LogHelper()
13         {
14         }
15         public static void SetConfig()
16         {
17             XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
18             Loginfo = LogManager.GetLogger(repository.Name, "loginfo");
19             Logerror = LogManager.GetLogger(repository.Name, "logerror");
20         }
21 
22         public static void SetConfig(FileInfo configFile)
23         {
24             log4net.Config.XmlConfigurator.Configure(repository, configFile);
25         }
26         /// <summary>
27         /// 普通的文件记录日志
28         /// </summary>
29         /// <param name="info"></param>
30         public static void WriteLog(string info)
31         {
32             SetConfig();
33             if (Loginfo.IsInfoEnabled)
34             {
35                 Loginfo.Info(info);
36             }
37         }
38         /// <summary>
39         /// 错误日志
40         /// </summary>
41         /// <param name="info"></param>
42         /// <param name="se"></param>
43         public static void WriteLog(string info, Exception se)
44         {
45             SetConfig();
46             if (Logerror.IsErrorEnabled)
47             {
48                 Logerror.Error(info, se);
49             }
50         }
51 
52     }

 

上一篇:log4日志组件在.netcore中的应用


下一篇:java开发struts2项目遇到FilterDispatcher过时