/// <summary> /// 写Log /// </summary> /// <param name="message">log内容</param> /// <param name="logMsgLevel">log类型</param> public static void Write(string message, LogMsgLevel logMsgLevel) { try { switch (logMsgLevel) { case LogMsgLevel.Debug: LogsHelper().Debug(message); break; case LogMsgLevel.Warn: LogsHelper().Warn(message); break; case LogMsgLevel.Info: LogsHelper().Info(message); break; case LogMsgLevel.Error: LogsHelper().Error(message); break; case LogMsgLevel.Fatal: LogsHelper().Fatal(message); break; } } catch (Exception ex) { WriteSystemLog(string.Format("写日志的时候发生错误,错误信息:{0}。需要写入日志的信息是:{1}!", ex.Message, message)); } }
/// <summary> /// 正常写日志;文件和数据库都写。 /// </summary> /// <param name="logInfo"></param> public void WriteLog(sys_log logInfo, LogMsgLevel logMsgLevel) { #region logInfo参数补充 if (logInfo == null) logInfo = new sys_log(); logInfo.create_date = DateTime.Now; logInfo.modify_date = logInfo.create_date; logInfo.status = 0; logInfo.local_ip = GetLocalIP(); logInfo.visitor_ip = GetClientIP(); #endregion //log4net按配置等级来写文本日志 LogHelper.Write(JsonHelper.ToJson(logInfo), logMsgLevel); //判断数据库日志级别,写数据库日志 var task = Task.Factory.StartNew( () => { try { logService.Add(logInfo); } catch (Exception ex) { LogHelper.Write("Famliy.Finance.BLL.LogBusiness.WriteLog() error.", ex); } }); }
/// <summary> /// 正常写日志;文件和数据库都写。 /// </summary> /// <param name="logInfo"></param> public void WriteLog(sys_log logInfo, LogMsgLevel logMsgLevel) { #region logInfo参数补充 if (logInfo == null) { logInfo = new sys_log(); } logInfo.create_date = DateTime.Now; logInfo.modify_date = logInfo.create_date; logInfo.status = 0; logInfo.local_ip = GetLocalIP(); logInfo.visitor_ip = GetClientIP(); #endregion //log4net按配置等级来写文本日志 LogHelper.Write(JsonHelper.ToJson(logInfo), logMsgLevel); //判断数据库日志级别,写数据库日志 var task = Task.Factory.StartNew( () => { try { logService.Add(logInfo); } catch (Exception ex) { LogHelper.Write("Famliy.Finance.BLL.LogBusiness.WriteLog() error.", ex); } }); }
/// <summary>Writes debug message into log file - if one is specified.</summary> /// <param name="msg">Text of the message.</param> /// <param name="origin">Name of the module/program part/method/class where this /// message was reported.</param> /// <param name="level">Level of message</param> void WriteMsg(string msg, LogMsgLevel level) { //Trace.WriteLine(msg); WriteToFile(msg, level); }
/// <summary>Writes provided information into log file. Message is slightly /// transformed before it is written to the disk - new line and tab characters /// are replaced with ' '. This way we produce /// valid tab-separated files, which can be viewed in e.g. Excel. Thread-safe.</summary> /// <param name="msg">Text of the message</param> /// <param name="level">Level of message</param> void WriteToFile(string msg, LogMsgLevel level) { if (dummy) { return; } if (level == LogMsgLevel.None) { throw new Exception("Illegal log message level - All."); } LogMsgLevel m_level = CurLevelProvider.Level; if (level < m_level) { return; } lock (this) { if (DoWeNeedNewLogFile()) { CreateNewFileName(); } // replace tabs and newlines with spaces msg = msg.Replace(Convert.ToChar(10), ' '); msg = msg.Replace(Convert.ToChar(13), ' '); msg = msg.Replace('\t', ' '); // find user and session descriptors if present string user = WindowsIdentity.GetCurrent().Name; string session = ""; if (msg[0] == '[') { int i = msg.IndexOf(']'); user = msg.Substring(0, i + 1); msg = msg.Substring(i + 1); if (msg[0] == '[') { int j = msg.IndexOf(']'); session = msg.Substring(0, j + 1); msg = msg.Substring(j + 1); } } // create message string string s = String.Format( "{0}:{1}\t{2}\t{3}\t{4}\t{5}", DateTime.Now.ToString(), DateTime.Now.Millisecond, System.Threading.Thread.CurrentThread.ManagedThreadId, user, session, msg); //write message to DB try { //using (UBSEntities db = UBSExtendedEntities.Create()) { // AuditLog log = new AuditLog(); // log.Application = "PURS_UBS Handler"; // log.LogTime = DateTime.Now; // log.Message = msg; // log.Severity = (int)level; // log.Username = user; // db.AddToAuditLog(log); // db.SaveChanges(); //} } catch (Exception ex) { Exception innerException = ex.InnerException; string message = String.Format("An error occured during processing!\nException: {0}\nStack Trace: {1}\nInner Exception: {2}", ex.Message, ex.StackTrace, innerException != null ? innerException.Message : String.Empty); //Logging.Singleton.WriteDebug(message); } // write message to file StreamWriter sw = new StreamWriter(file_name_current, true, Encoding.Unicode); // append to file try { sw.WriteLine(s); } finally { sw.Close(); }; } }
/// <summary>Writes provided information into log file. Message is slightly /// transformed before it is written to the disk - new line and tab characters /// are replaced with ' '. This way we produce /// valid tab-separated files, which can be viewed in e.g. Excel. Thread-safe.</summary> /// <param name="msg">Text of the message</param> /// <param name="level">Level of message</param> void WriteToFile(string msg, LogMsgLevel level) { if (dummy) return; if (level == LogMsgLevel.None) throw new Exception("Illegal log message level - All."); LogMsgLevel m_level = CurLevelProvider.Level; if (level < m_level) return; lock (this) { if (DoWeNeedNewLogFile()) CreateNewFileName(); // replace tabs and newlines with spaces msg = msg.Replace(Convert.ToChar(10), ' '); msg = msg.Replace(Convert.ToChar(13), ' '); msg = msg.Replace('\t', ' '); // find user and session descriptors if present string user = WindowsIdentity.GetCurrent().Name; string session = ""; if (msg[0] == '[') { int i = msg.IndexOf(']'); user = msg.Substring(0, i + 1); msg = msg.Substring(i + 1); if (msg[0] == '[') { int j = msg.IndexOf(']'); session = msg.Substring(0, j + 1); msg = msg.Substring(j + 1); } } // create message string string s = String.Format( "{0}:{1}\t{2}\t{3}\t{4}\t{5}", DateTime.Now.ToString(), DateTime.Now.Millisecond, System.Threading.Thread.CurrentThread.ManagedThreadId, user, session, msg); //write message to DB try { //using (UBSEntities db = UBSExtendedEntities.Create()) { // AuditLog log = new AuditLog(); // log.Application = "PURS_UBS Handler"; // log.LogTime = DateTime.Now; // log.Message = msg; // log.Severity = (int)level; // log.Username = user; // db.AddToAuditLog(log); // db.SaveChanges(); //} } catch (Exception ex) { Exception innerException = ex.InnerException; string message = String.Format("An error occured during processing!\nException: {0}\nStack Trace: {1}\nInner Exception: {2}", ex.Message, ex.StackTrace, innerException != null ? innerException.Message : String.Empty); //Logging.Singleton.WriteDebug(message); } // write message to file StreamWriter sw = new StreamWriter(file_name_current, true,Encoding.Unicode); // append to file try { sw.WriteLine(s); } finally { sw.Close(); }; } }
/// <summary> /// 写Log /// </summary> /// <param name="message">log内容</param> /// <param name="logMsgLevel">log类型</param> public static void Write(string message, LogMsgLevel logMsgLevel) { try { switch (logMsgLevel) { case LogMsgLevel.Debug: LogsHelper().Debug(message); break; case LogMsgLevel.Info: LogsHelper().Info(message); break; case LogMsgLevel.Warn: LogsHelper().Warn(message); break; case LogMsgLevel.Error: LogsHelper().Error(message); break; case LogMsgLevel.Fatal: LogsHelper().Fatal(message); break; } } catch (Exception ex) { WriteSystemLog(string.Format("写日志的时候发生错误,错误信息:{0}。需要写入日志的信息是:{1}!", ex.Message, message)); } }