protected virtual StringBuilder BuildLogMessage(LogThreshold threshold, string format, params object[] args) { var builder = new StringBuilder(); if (PrefixLog) { builder.Append(threshold.ToString().ToUpper()); if (!string.IsNullOrEmpty(Prefix)) { builder.Append(" "); builder.Append(Prefix); } else { builder.Append(" "); builder.Append(DateTime.Now.ToString("g")); } builder.Append(" "); } builder.AppendFormat(format, args); builder.Append('\n'); return(builder); }
/// <summary> /// Log using the Log 4 /// </summary> /// <param name="message"></param> /// <param name="exceptionData"></param> /// <param name="methodName"></param> /// <param name="threshold"></param> public static void LogException(Exception ex, string methodName, LogThreshold threshold) { ILog Logger = LogManager.GetLogger(methodName); try { switch (threshold) { case LogThreshold.INFO: { Logger.Info(ex.Message, ex); break; } case LogThreshold.WARN: { Logger.Warn(ex.Message, ex); break; } case LogThreshold.ERROR: { Logger.Error(ex.Message, ex); break; } case LogThreshold.FATAL: { Logger.Fatal(ex.Message, ex); break; } case LogThreshold.DEBUG: { Logger.Debug(ex.Message, ex); break; } default: { Logger.Info(ex.Message, ex); break; } } } catch (Exception exc) { try { Logger.Error("Error while logging using log4Net parameters:Message=" + ex.Message + " ,MethodName=" + methodName, exc); } catch { } } }
protected virtual void Write(LogThreshold threshold, string format, params object[] args) { if (Threshold.HasFlag(threshold)) { var message = BuildLogMessage(threshold, format, args); var buf = _encoding.GetBytes(message.ToString()); stream.Write(buf, 0, buf.Length); stream.Flush(); } }