使用log4net记录日志到数据库(含自定义属性)

 日志输出自定义属性! 特来总结一下:

 

一、配置文件

使用log4写入数据库就不多说了,网上方法很多,自定义字段如下

1 <commandText value="INSERT INTO dbo.AppLog (dtDate,sThread,sLevel,sLogger,sMessage,sException,ip,url) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception,@userIP,@url)" />
1 <parameter>
2 <parameterName value="@userIP" />
3 <dbType value="String" />
4 <size value="50" />
5 <layout type="NNAppServer.Common.MyLayout" >
6 <conversionPattern value = "%property{userIP}"/> 
7 </layout>
8 </parameter>
NNAppServer.Common.MyLayout  是要自定义类的命名空间

二、自定义类
新建 MyCustomer 类 ,注意命名空间和配置文件一致
 1 public class MyCustomer
 2     {
 3         public MyCustomer(string userIP, string url)
 4         {
 5             this._userIP = userIP;
 6             this._url = url;
 7         }
 8 
 9         private string _userIP;
10 
11         /// <summary>
12         /// 用户IP
13         /// </summary>
14         public string userIP
15         {
16             get { return _userIP; }
17             set { _userIP = value; }
18         }
19 
20         private string _url;
21 
22         /// <summary>
23         /// 路径
24         /// </summary>
25         public string url
26         {
27             get { return _url; }
28             set { _url = value; }
29         }
30     }
31 
32     class MyLayout : PatternLayout
33     {
34         public MyLayout()
35         {
36             this.AddConverter("property", typeof(MyMessagePatternConverter));
37         }
38     }
39 
40     class MyMessagePatternConverter : PatternLayoutConverter
41     {
42         protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
43         {
44             if (Option != null)
45             {
46                 // Write the value for the specified key
47                 WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
48             }
49             else
50             {
51                 // Write all the key value pairs
52                 WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
53             }
54         }
55 
56         private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent)
57         {
58             object propertyValue = string.Empty;
59 
60             var pi = loggingEvent.MessageObject.GetType().GetProperty(property);
61 
62             if (pi != null)
63                 propertyValue = pi.GetValue(loggingEvent.MessageObject, null);
64 
65             return propertyValue;
66         }

 

三、新建一个异常过滤器,将要获取的字段写入

public class MyExceptionAttribute : IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            var ex = filterContext.Exception;
            var ip = Common.ComHelper.GetIP();
            string url = filterContext.HttpContext.Request.RawUrl;

            LogHelper.Error(new MyCustomer(ip, url), ex);
        }
    }

 

参考文档: http://www.cnblogs.com/goody9807/archive/2010/10/29/1864581.html

 

处理webApi: 1、自定义一个类,继承ExceptionFilterAttribute,重写OnException方法  和mvc差不多

       2、在 WebApiConfig.cs 文件的 Register方法里面添加  config.Filters.Add(new WebApiExceptionFilterAttribute())

https://www.cnblogs.com/landeanfen/p/5363846.html  

 

 

http://blog.sina.com.cn/s/blog_72463843010195ru.html     

空合并运算符【 ?? 】为右结合运算符,即操作时从右向左进行组合的


posted @ 2017-11-06 15:00  查克拉的觉醒  阅读(374)  评论(0编辑  收藏  举报