1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using log4net;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Web;
- namespace Central.Control.WebApi.Log4net
- {
- /// <summary>
- /// 日志工具类
- /// </summary>
- public class Log4NetHelper: ILog4NetHelper
- {
- public Log4NetHelper()
- {
- log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/Config/log4net.config")));
- }
- public void Debug(object message)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);
- }
- public void Debug(object message, Exception ex)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);
- }
- public void Error(object message)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);
- }
- public void Error(object message, Exception exception)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);
- }
- private string GetCurrentMethodFullName()
- {
- try
- {
- StackFrame frame;
- string str2;
- int num = 2;
- StackTrace trace = new StackTrace();
- int length = trace.GetFrames().Length;
- do
- {
- frame = trace.GetFrame(num++);
- str2 = frame.GetMethod().DeclaringType.ToString();
- }
- while (str2.EndsWith("Exception") && (num < length));
- string name = frame.GetMethod().Name;
- return (str2 + "." + name);
- }
- catch
- {
- return null;
- }
- }
- public void Info(object message)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);
- }
- public void Info(object message, Exception ex)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);
- }
- public void Warn(object message)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);
- }
- public void Warn(object message, Exception ex)
- {
- LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);
- }
- }
- }
|