From 202cdf4c400f7b8507f129fab5766baac6bc7e45 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期二, 11 三月 2025 17:40:20 +0800 Subject: [PATCH] 1 --- 项目代码/WMS/WIDESEA_WMSServer/LogLibrary/Log/Log.cs | 755 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 755 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/LogLibrary/Log/Log.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/LogLibrary/Log/Log.cs" new file mode 100644 index 0000000..f636155 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/LogLibrary/Log/Log.cs" @@ -0,0 +1,755 @@ +锘縰sing System; +using System.Text.RegularExpressions; + +namespace LogLibrary.Log +{ + + public unsafe class Log + { + private string m_Name; + private const string m_MessageTemplate = "{0}-{1}: {2}"; + + private const string m_Debug = "DEBUG"; + + private const string m_Error = "ERROR"; + + private const string m_Fatal = "FATAL"; + + private const string m_Info = "INFO"; + + private const string m_Warn = "WARN"; + + /// <summary> + /// Initializes a new instance of the <see cref="Log"/> class. + /// </summary> + /// <param name="name">The name.</param> + public Log(string name) + { + m_Name = name; + } + public Log() + { + m_Name = "NaN"; + } + + /// <summary> + /// Gets a value indicating whether this instance is debug enabled. + /// </summary> + /// <value> + /// <c>true</c> if this instance is debug enabled; otherwise, <c>false</c>. + /// </value> + public bool IsDebugEnabled + { + get { return true; } + } + + /// <summary> + /// Gets a value indicating whether this instance is error enabled. + /// </summary> + /// <value> + /// <c>true</c> if this instance is error enabled; otherwise, <c>false</c>. + /// </value> + public bool IsErrorEnabled + { + get { return true; } + } + + /// <summary> + /// Gets a value indicating whether this instance is fatal enabled. + /// </summary> + /// <value> + /// <c>true</c> if this instance is fatal enabled; otherwise, <c>false</c>. + /// </value> + public bool IsFatalEnabled + { + get { return true; } + } + + /// <summary> + /// Gets a value indicating whether this instance is info enabled. + /// </summary> + /// <value> + /// <c>true</c> if this instance is info enabled; otherwise, <c>false</c>. + /// </value> + public bool IsInfoEnabled + { + get { return true; } + } + + /// <summary> + /// Gets a value indicating whether this instance is warn enabled. + /// </summary> + /// <value> + /// <c>true</c> if this instance is warn enabled; otherwise, <c>false</c>. + /// </value> + public bool IsWarnEnabled + { + get { return true; } + } + + + + public string GetDataTimeLog(string log) + { + return string.Format("[{0}] >> {1}", DateTime.Now.ToString("yy-MM-dd HH:mm:ss"), log); + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + public void Debug(bool isWriteFile, object message) + { + string log = GetDataTimeLog(message.ToString()); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + /// <param name="exception">The exception.</param> + public void Debug(bool isWriteFile, object message, Exception exception) + { + string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + public void DebugFormat(bool isWriteFile, string format, object arg0) + { + string log = GetDataTimeLog(string.Format(format, arg0)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void DebugFormat(bool isWriteFile, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(format, args)); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="provider">The provider.</param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(provider, format, args)); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the debug message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + /// <param name="arg2">The arg2.</param> + public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Debug, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + public void Error(bool isWriteFile, object message) + { + string log = GetDataTimeLog(message.ToString()); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + /// <param name="exception">The exception.</param> + public void Error(bool isWriteFile, object message, Exception exception) + { + //string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); + string log = GetDataTimeLog(message + Environment.NewLine + exception.Message ); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + public void ErrorFormat(bool isWriteFile, string format, object arg0) + { + string log = GetDataTimeLog(string.Format(format, arg0)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void ErrorFormat(bool isWriteFile, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="provider">The provider.</param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(provider, format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + /// <param name="arg2">The arg2.</param> + public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg2)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Error, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + public void Fatal(bool isWriteFile, object message) + { + string log = GetDataTimeLog(message.ToString()); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + /// <param name="exception">The exception.</param> + public void Fatal(bool isWriteFile, object message, Exception exception) + { + string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + public void FatalFormat(bool isWriteFile, string format, object arg0) + { + string log = GetDataTimeLog(string.Format(format, arg0)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void FatalFormat(bool isWriteFile, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="provider">The provider.</param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(provider, format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the fatal error message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + /// <param name="arg2">The arg2.</param> + public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Fatal, log); + } + } + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + public void Info(bool isWriteFile, object message) + { + string log = GetDataTimeLog(message.ToString()); + Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Info, log); + } + } + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + /// <param name="exception">The exception.</param> + public void Info(bool isWriteFile, object message, Exception exception) + { + string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); + Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Info, log); + } + } + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + public void InfoFormat(bool isWriteFile, string format, object arg0) + { + string log = GetDataTimeLog(string.Format(format, arg0)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Info, log); + } + } + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + //public void InfoFormat(bool isWriteFile, string format, params object[] args) + //{ + // string log = GetDataTimeLog(string.Format(format, args)); + // Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + // if (isWriteFile) + // { + // LogUtil.WriteLogFile(m_Name, m_Info, log); + // } + //} + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="provider">The provider.</param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + //public void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) + //{ + // string log = GetDataTimeLog(string.Format(provider, format, args)); + // Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + // if (isWriteFile) + // { + // LogUtil.WriteLogFile(m_Name, m_Info, log); + // } + //} + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + //public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1) + //{ + // string log = GetDataTimeLog(string.Format(format, arg0, arg1)); + // Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + // if (isWriteFile) + // { + // LogUtil.WriteLogFile(m_Name, m_Info, log); + // } + //} + + /// <summary> + /// Logs the info message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + /// <param name="arg2">The arg2.</param> + public void InfoFormat(bool isWriteFile, object arg0, object arg1, object arg2) + { + string func(string str, int len) + { + var strSLen = Regex.Replace(str, @"[^\x00-\xff]", "aa").Length; + var strLen = str.Length; + return str.PadLeft(len - strSLen + strLen); + } + string log = GetDataTimeLog(string.Format("[{0}]\t[{1}]\t{2}", arg0?.ToString(), arg1?.ToString(), arg2)); + //string log = GetDataTimeLog(string.Format("[{0}] [{1}] {2}", func(arg0?.ToString(), 13), func(arg1?.ToString(), 16), arg2)); + //Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Info, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + public void Warn(bool isWriteFile, object message) + { + string log = GetDataTimeLog(message.ToString()); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="message">The message.</param> + /// <param name="exception">The exception.</param> + public void Warn(bool isWriteFile, object message, Exception exception) + { + string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + public void WarnFormat(bool isWriteFile, string format, object arg0) + { + string log = GetDataTimeLog(string.Format(format, arg0)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void WarnFormat(bool isWriteFile, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="provider">The provider.</param> + /// <param name="format">The format.</param> + /// <param name="args">The args.</param> + public void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) + { + string log = GetDataTimeLog(string.Format(provider, format, args)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + /// <summary> + /// Logs the warning message. + /// </summary> + /// <param name="isWriteFile"></param> + /// <param name="format">The format.</param> + /// <param name="arg0">The arg0.</param> + /// <param name="arg1">The arg1.</param> + /// <param name="arg2">The arg2.</param> + public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) + { + string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); + Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); + if (isWriteFile) + { + LogUtil.WriteLogFile(m_Name, m_Warn, log); + } + } + + //public void Behavior(string logMsg, Level level) + //{ + // lock (this) + // { + // m_Name = "琛屼负"; + // switch (level) + // { + // case Level.Debug: + // Debug(true, logMsg); + // break; + // case Level.Info: + // Info(true, logMsg); + // break; + // case Level.Warning: + // Warn(true, logMsg); + // break; + // case Level.Error: + // Error(true, logMsg); + // break; + // default: + // break; + // } + // } + //} + + //public void Interface(string logMsg, Level level) + //{ + // lock (this) + // { + // m_Name = "鎺ュ彛"; + // switch (level) + // { + // case Level.Debug: + // Debug(true, logMsg); + // break; + // case Level.Info: + // Info(true, logMsg); + // break; + // case Level.Warning: + // Warn(true, logMsg); + // break; + // case Level.Error: + // Error(true, logMsg); + // break; + // default: + // break; + // } + // } + //} + + //public void Hardware(string logMsg, Level level) + //{ + // lock (this) + // { + // m_Name = "纭欢"; + // switch (level) + // { + // case Level.Debug: + // Debug(true, logMsg); + // break; + // case Level.Info: + // Info(true, logMsg); + // break; + // case Level.Warning: + // Warn(true, logMsg); + // break; + // case Level.Error: + // Error(true, logMsg); + // break; + // default: + // break; + // } + // } + //} + } +} -- Gitblit v1.9.3