博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Log4net插件
阅读量:6324 次
发布时间:2019-06-22

本文共 4012 字,大约阅读时间需要 13 分钟。

基本代码

 

using System;using System.Collections.Generic;	 using System.Linq;using System.Text;using log4net.Util;using System.IO;using System.Net;using Log4netUTS.Model;namespace log4netUTS{    ///     ///     /// 
///
///
///
///
///
///
///
///
public class HttpAppender : log4net.Appender.AppenderSkeleton { public HttpAppender() { Timer = 5000; MaxRecords = 300; } private System.Threading.Timer mTimer; private void CreateTime() { lock (this) { if (mTimer == null) mTimer = new System.Threading.Timer(Upload, null, Timer, Timer); } } private readonly static Type declaringType = typeof(HttpAppender); private Queue
mEvents = new Queue
(1000); private void Add(LogEvent item) { CreateTime(); lock (mEvents) { mEvents.Enqueue(item); if (mEvents.Count > MaxRecords) Upload(null); } } private void Upload(object state) { List
items = new List
(); lock (mEvents) { while (mEvents.Count > 0) { items.Add(mEvents.Dequeue()); } } if (items.Count > 0) { OnUpload(items); } } private void OnUpload(object state) { try { List
items = (List
)state; string param =string.Format("UserName={0}&UserPwd={1}&LogData={2}",UserName,UserPWD, Newtonsoft.Json.JsonConvert.SerializeObject(items)); byte[] data = Encoding.UTF8.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Host); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); } using (WebResponse wr = req.GetResponse()) { } } catch (Exception e_) { LogLog.Error(declaringType, e_.Message); } } public string Host { get; set; } public string ServerTag { get; set; } public string AppName { get; set; } public int MaxRecords { get; set; } public int Timer { get; set; } public string UserName { get; set; } public string UserPWD { get; set; } protected override void Append(log4net.Core.LoggingEvent loggingEvent) { try { LogEvent le = new LogEvent(); le.ErrorTime =loggingEvent.TimeStamp.ToString("yyyy-MM-dd HH:mm:ss"); le.EventType = loggingEvent.Level.ToString(); le.Message = loggingEvent.RenderedMessage; le.AppName = AppName; le.ServerTag = ServerTag; le.ErrorType = loggingEvent.ExceptionObject != null ? loggingEvent.ExceptionObject.GetType().ToString() : "未知错误类型"; AddError(le, loggingEvent.ExceptionObject); Add(le); } catch (Exception e_) { LogLog.Error(declaringType, e_.Message); } } private void AddError(LogEvent e, Exception err) { if (err != null) { e.Errors.Add(new EventMessage { Message = err.Message, StackTrace = err.StackTrace }); err = err.InnerException; while (err != null) { e.Errors.Add(new EventMessage { Message = err.Message, StackTrace = err.StackTrace }); err = err.InnerException; } } } }}

使用配置:

 

 

 

里面可能有些model的引用,根据自己的项目情况,写个就行了!

而且使用了第三方的序列组件 Newtonsoft.Json.Net35

 

转载地址:http://vvpaa.baihongyu.com/

你可能感兴趣的文章
variant conversion error for variable: V67 (或其它数字)
查看>>
MyBatis学习总结(七)——Mybatis缓存
查看>>
RabbitMQ学习总结(一)——基础概念详细介绍
查看>>
关于ls vim查看中文乱码问题
查看>>
FileReader读取文件编码丢失问题(乱码)
查看>>
国内DNS方面的技术力量
查看>>
ORACLE排错记录
查看>>
我热爱编程,但我讨厌这个行业
查看>>
alwayson高可用性组
查看>>
都是编译器惹的祸
查看>>
文件权限
查看>>
route命令
查看>>
用 cgroups 管理 cpu 资源
查看>>
MySQL各版本特性及其价格
查看>>
11g新特性:RDBMS Component TRACE
查看>>
jquery 修改排序
查看>>
Iterator接口
查看>>
嵌入式开发的学习步骤及入门
查看>>
决心书
查看>>
Oracle 基础篇 --- B树索引内部结构
查看>>