-
-
Save pisceanfoot/2ae006f7ace346ebce41 to your computer and use it in GitHub Desktop.
log use log4net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// | |
/// </summary> | |
public static class Log | |
{ | |
private static ILog logger; | |
static Log() | |
{ | |
string log4NetConfigPath = AppDomain.CurrentDomain.BaseDirectory + "//Log4Net.config"; | |
if (File.Exists(log4NetConfigPath)) | |
{ | |
XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigPath)); | |
} | |
logger = LogManager.GetLogger("CN.Newegg.UIH.Agent"); | |
if (logger.IsDebugEnabled) | |
{ | |
logger.DebugFormat("The trace level is currently set to debug. " | |
+ "This will cause agent to log at the most verbose level, which is useful for setting up or debugging the server. " | |
+ "Once your server is running smoothly, we recommend changing this setting in {0} to a lower level.", | |
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); | |
} | |
} | |
/// <summary> | |
/// Logs at information level | |
/// </summary> | |
/// <param name="message"></param> | |
/// <param name="args"></param> | |
public static void Info(string message, params object[] args) | |
{ | |
logger.Info(string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
/// <summary> | |
/// Logs at information level | |
/// </summary> | |
/// <param name="message"></param> | |
public static void Info(string message) | |
{ | |
logger.Info(message); | |
} | |
/// <summary> | |
/// Logs at debug level | |
/// </summary> | |
/// <param name="message"></param> | |
public static void Debug(string message) | |
{ | |
logger.Debug(message); | |
} | |
/// <summary> | |
/// Logs at debug level | |
/// </summary> | |
/// <param name="message"></param> | |
/// <param name="args"></param> | |
public static void Debug(string message, params object[] args) | |
{ | |
logger.Debug(string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
/// <summary> | |
/// Logs at warning level | |
/// </summary> | |
/// <param name="message"></param> | |
public static void Warning(string message) | |
{ | |
logger.Warn(message); | |
} | |
/// <summary> | |
/// Logs at warning level | |
/// </summary> | |
/// <param name="message"></param> | |
/// <param name="args"></param> | |
public static void Warning(string message, params object[] args) | |
{ | |
logger.Warn(string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
/// <summary> | |
/// Logs at warning level | |
/// </summary> | |
/// <param name="ex"></param> | |
public static void Warning(Exception ex) | |
{ | |
logger.Warn(CreateExceptionMessage(ex)); | |
} | |
/// <summary> | |
/// Logs at Error level | |
/// </summary> | |
/// <param name="message"></param> | |
public static void Error(string message) | |
{ | |
logger.Error(message); | |
} | |
/// <summary> | |
/// Logs at error level | |
/// </summary> | |
/// <param name="message"></param> | |
/// <param name="args"></param> | |
public static void Error(string message, params object[] args) | |
{ | |
logger.Error(string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
/// <summary> | |
/// Logs at errorlevel | |
/// </summary> | |
/// <param name="ex"></param> | |
public static void Error(Exception ex) | |
{ | |
logger.Error(CreateExceptionMessage(ex)); | |
} | |
/// <summary> | |
/// Logs at fatal level | |
/// </summary> | |
/// <param name="message"></param> | |
public static void Fatal(string message) | |
{ | |
logger.Fatal(message); | |
} | |
/// <summary> | |
/// Logs at fatal level | |
/// </summary> | |
/// <param name="message"></param> | |
/// <param name="args"></param> | |
public static void Fatal(string message, params object[] args) | |
{ | |
logger.Fatal(string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
/// <summary> | |
/// Logs at fatal level | |
/// </summary> | |
/// <param name="ex"></param> | |
public static void Fatal(Exception ex) | |
{ | |
logger.Fatal(CreateExceptionMessage(ex)); | |
} | |
/// <summary> | |
/// Starts the trace. | |
/// </summary> | |
/// <returns></returns> | |
/// <remarks></remarks> | |
public static TraceBlock StartTrace() | |
{ | |
return new TraceBlock(logger, GetCallingClassName()); | |
} | |
/// <summary> | |
/// Starts the trace. | |
/// </summary> | |
/// <param name="message">The message.</param> | |
/// <param name="args">The args.</param> | |
/// <returns></returns> | |
/// <remarks></remarks> | |
public static TraceBlock StartTrace(string message, params object[] args) | |
{ | |
return new TraceBlock(logger, GetCallingClassName(), string.Format(CultureInfo.CurrentCulture, message, args)); | |
} | |
private static string GetCallingClassName() | |
{ | |
StackTrace stack = default(StackTrace); | |
StackFrame currentFrame = default(StackFrame); | |
string myAssemblyName = null; | |
string myClassName = null; | |
string myMethodName = null; | |
try | |
{ | |
stack = new StackTrace(); | |
currentFrame = stack.GetFrame(2); | |
myAssemblyName = currentFrame.GetMethod().ReflectedType.Assembly.FullName.Split(',')[0]; | |
myClassName = currentFrame.GetMethod().ReflectedType.ToString(); | |
myMethodName = currentFrame.GetMethod().Name; | |
} | |
catch | |
{ | |
myClassName = ""; | |
myMethodName = ""; | |
} | |
return string.Format(System.Globalization.CultureInfo.CurrentCulture, | |
"{0} - {1}.{2} : ", | |
myAssemblyName, myClassName, myMethodName); | |
} | |
private static string CreateExceptionMessage(Exception ex) | |
{ | |
if (ex is ThreadAbortException) | |
{ | |
return "Thread aborted for Project: " + Thread.CurrentThread.Name; | |
} | |
StringBuilder buffer = new StringBuilder(); | |
buffer.Append(ex.Message).Append(Environment.NewLine); | |
buffer.Append("----------").Append(Environment.NewLine); | |
buffer.Append(ex.ToString()).Append(Environment.NewLine); | |
buffer.Append("----------").Append(Environment.NewLine); | |
return buffer.ToString(); | |
} | |
/// <summary> | |
/// A class for putting a trace call in a block. | |
/// </summary> | |
public class TraceBlock | |
: IDisposable | |
{ | |
private string methodName; | |
private ILog logger; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="TraceBlock"/> class. | |
/// </summary> | |
/// <param name="logger">The underlying logger to use.</param> | |
/// <param name="methodName">The name of the method that is being traced;</param> | |
public TraceBlock(ILog logger, string methodName) | |
{ | |
this.logger = logger; | |
this.methodName = methodName; | |
if (this.logger.IsDebugEnabled) | |
{ | |
this.logger.DebugFormat("Entering {0}", methodName); | |
} | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="TraceBlock"/> class. | |
/// </summary> | |
/// <param name="logger">The underlying logger to use.</param> | |
/// <param name="methodName">The name of the method that is being traced;</param> | |
/// <param name="message"></param> | |
public TraceBlock(ILog logger, string methodName, string message) | |
{ | |
this.logger = logger; | |
this.methodName = methodName; | |
if (this.logger.IsDebugEnabled) | |
{ | |
this.logger.DebugFormat("Entering {0}: {1}", methodName, message); | |
} | |
} | |
/// <summary> | |
/// Disposes of the trace block. | |
/// </summary> | |
public void Dispose() | |
{ | |
if (this.logger.IsDebugEnabled) | |
{ | |
this.logger.DebugFormat("Exiting {0}", methodName); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment