From e34633edeadd22cdd5b81357f909198533230585 Mon Sep 17 00:00:00 2001 From: hzhuangxin01 Date: Mon, 18 Feb 2019 19:21:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E4=BD=BF=E7=94=A8=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=98=AF=E7=B3=BB=E7=BB=9F=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- GeneratorCode/App.config | 2 +- GeneratorCode/Configure/NConfig.cs | 12 +-- GeneratorCode/GeneratorCode.csproj | 2 +- GeneratorCode/Logs/NLog.cs | 117 ++++++++++++++++++++++++++--- GeneratorCode/Program.cs | 65 ++++++++++------ GeneratorCode/config.ini | 26 ++++++- 7 files changed, 181 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 4d1eb44..93011da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs/ /GeneratorCode/obj /GeneratorCode/bin/Debug -/TmatrixCodeGenerator/ \ No newline at end of file +/TmatrixCodeGenerator/ +/GeneratorCode/bin/Release diff --git a/GeneratorCode/App.config b/GeneratorCode/App.config index 877624e..4543795 100644 --- a/GeneratorCode/App.config +++ b/GeneratorCode/App.config @@ -1,6 +1,6 @@ - + diff --git a/GeneratorCode/Configure/NConfig.cs b/GeneratorCode/Configure/NConfig.cs index ced9a2e..16ec06c 100644 --- a/GeneratorCode/Configure/NConfig.cs +++ b/GeneratorCode/Configure/NConfig.cs @@ -51,15 +51,15 @@ namespace GeneratorCode.Configure var cfgTask = new List(); var appPath = Process.GetCurrentProcess().MainModule.FileName; - Trace.WriteLine("Application: " + appPath); - Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath)); - Trace.WriteLine("File: " + Path.GetFileName(appPath)); + //Trace.WriteLine("Application: " + appPath); + //Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath)); + //Trace.WriteLine("File: " + Path.GetFileName(appPath)); - var fw = new FileSystemWatcher(@"E:\", @"my.cfg") + var fw = new FileSystemWatcher() { NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite, - //Filter = _cfgFileName, - //Path = Path.GetDirectoryName(appPath), + Filter = _cfgFileName, + Path = Path.GetDirectoryName(appPath), IncludeSubdirectories = false, }; diff --git a/GeneratorCode/GeneratorCode.csproj b/GeneratorCode/GeneratorCode.csproj index 6cd812b..ef361c9 100644 --- a/GeneratorCode/GeneratorCode.csproj +++ b/GeneratorCode/GeneratorCode.csproj @@ -8,7 +8,7 @@ Exe GeneratorCode GeneratorCode - v4.5 + v4.6.1 512 true true diff --git a/GeneratorCode/Logs/NLog.cs b/GeneratorCode/Logs/NLog.cs index f242547..d689c5c 100644 --- a/GeneratorCode/Logs/NLog.cs +++ b/GeneratorCode/Logs/NLog.cs @@ -45,11 +45,20 @@ namespace GeneratorCode.Logs OnLogCfgChanged?.Invoke(); } + public bool LogEnable { get; set; } public NLogLevel LogLevel { get; set; } public NLogLevel DefaultLevel { get; set; } public bool AsyncMode { get; set; } public bool ForceNewLine { get; set; } + public bool ShowDate { get; set; } + public bool ShowTime { get; set; } + public bool ShowMSec { get; set; } + public bool ShowLevel { get; set; } + public bool ShowCodeFile { get; set; } + public bool ShowFunction { get; set; } + public bool ShowCodeLine { get; set; } + public bool EnConsole { get; set; } public bool EnTrace { get; set; } public bool EnDebug { get; set; } @@ -61,7 +70,7 @@ namespace GeneratorCode.Logs public int SleepTime { get; set; } public int NumOutItems { get; set; } - public string Path { get; set; } + public string DirPath { get; set; } public string FileNamePre { get; set; } public bool EnSplitLog { get; set; } public bool SplitByData { get; set; } @@ -73,11 +82,20 @@ namespace GeneratorCode.Logs { lock (_cfgLock) { + LogEnable = NConfig.GetCfgValue("LogGlobal", "LogEnable", false); LogLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "LogLevel", (int)NLogLevel.MaxLevel); DefaultLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "DefaultLogLevel", (int)NLogLevel.Info); AsyncMode = NConfig.GetCfgValue("LogGlobal", "AsyncMode", false); ForceNewLine = NConfig.GetCfgValue("LogGlobal", "AutoForceNewLine", false); + ShowDate = NConfig.GetCfgValue("LogFormat", "ShowDate", true); + ShowTime = NConfig.GetCfgValue("LogFormat", "ShowTime", true); + ShowMSec = NConfig.GetCfgValue("LogFormat", "ShowMSec", true); + ShowLevel = NConfig.GetCfgValue("LogFormat", "ShowLevel", true); + ShowCodeFile = NConfig.GetCfgValue("LogFormat", "ShowCodeFile", true); + ShowFunction = NConfig.GetCfgValue("LogFormat", "ShowFunction", true); + ShowCodeLine = NConfig.GetCfgValue("LogFormat", "ShowCodeLine", true); + EnConsole = NConfig.GetCfgValue("LogOutput", "Console", true); EnTrace = NConfig.GetCfgValue("LogOutput", "Trace", false); EnDebug = NConfig.GetCfgValue("LogOutput", "Debug", true); @@ -90,13 +108,25 @@ namespace GeneratorCode.Logs if (MaxItemsCache == 0) MaxItemsCache = int.MaxValue; CacheMode = (CacheOptMode)NConfig.GetCfgValue("AsyncLogSetting", "CacheFullOpts", (int)CacheOptMode.Drop); - SleepTime = NConfig.GetCfgValue("AsyncLogSetting", "ThreadSleep", 10); NumOutItems = NConfig.GetCfgValue("AsyncLogSetting", "NumItemsOutEachTime", 10); } + SleepTime = NConfig.GetCfgValue("AsyncLogSetting", "ThreadSleep", 10); + if (EnFile) { - Path = NConfig.GetCfgValue("FileLogSetting", "Path", @"./"); + DirPath = NConfig.GetCfgValue("FileLogSetting", "Path", @""); + + if (DirPath == "" || !Directory.Exists(DirPath) || DirPath == @".\") + { + DirPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\"; + } + + if (!Directory.Exists(DirPath)) + { + Directory.CreateDirectory(DirPath); + } + FileNamePre = NConfig.GetCfgValue("FileLogSetting", "FileNamePrefix", ""); EnSplitLog = NConfig.GetCfgValue("FileLogSetting", "AutoSplitFile", true); @@ -170,7 +200,7 @@ namespace GeneratorCode.Logs if (_logCfg.EnFile) { _logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}", - _logCfg.Path, + _logCfg.DirPath, _logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "", Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName), DateTime.Now, @@ -197,6 +227,15 @@ namespace GeneratorCode.Logs } } + public static void NLog_Finish() + { + if (_logCfg.EnFile) + { + _logSw?.Flush(); + _logSw?.Close(); + } + } + public static void NLog_Init() { _logCfg = new NLogConfig(); @@ -241,7 +280,7 @@ namespace GeneratorCode.Logs if (_logCfg.EnSplitLog) { - bool isNeedSplit = true; + bool isNeedSplit = false; if (_logCfg.SplitByData && lastDt.Day != DateTime.Now.Day) { @@ -263,7 +302,7 @@ namespace GeneratorCode.Logs Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName)); _logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}", - _logCfg.Path, + _logCfg.DirPath, _logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "", Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName), DateTime.Now, @@ -279,7 +318,7 @@ namespace GeneratorCode.Logs _logFileName += ".log"; parttn += ".log"; - foreach (var f in Directory.GetFiles(_logCfg.Path, parttn, SearchOption.TopDirectoryOnly)) + foreach (var f in Directory.GetFiles(_logCfg.DirPath, parttn, SearchOption.TopDirectoryOnly)) { File.Delete(f); Trace.WriteLine("Delect Rollback log: " + f); @@ -311,9 +350,63 @@ namespace GeneratorCode.Logs private static string LogFormat(NLogLevel logLevel, string logMsg, string fileName, string funName, int lineNo, DateTime dt) { - var msg = "[" + dt.ToString("yyyy-MM-dd HH:mm:ss.fff") - + "] [" + LogLevelToString(logLevel) + "] [" + Path.GetFileName(fileName) + "] - " - + funName + "(" + lineNo + "):" + logMsg; + var msg = ""; + + if (_logCfg.ShowDate || _logCfg.ShowTime) + { + msg += "["; + } + + if (_logCfg.ShowDate) + { + msg += dt.ToString("yyyy-MM-dd"); + + if (_logCfg.ShowTime) + { + msg += " "; + } + } + + if (_logCfg.ShowTime) + { + msg += dt.ToString("HH:mm:ss"); + + if (_logCfg.ShowMSec) + { + msg += "." + dt.ToString("fff"); + } + } + + if (_logCfg.ShowDate || _logCfg.ShowTime) + { + msg += "]"; + } + + if (_logCfg.ShowLevel) + { + msg += " [" + LogLevelToString(logLevel) + "] "; + } + + if (_logCfg.ShowCodeFile) + { + msg += "[" + Path.GetFileName(fileName) + "] "; + } + + if (_logCfg.ShowFunction) + { + msg += "- " + funName; + } + + if (_logCfg.ShowCodeFile || _logCfg.ShowFunction) + { + if (_logCfg.ShowCodeLine) + { + msg += "(" + lineNo.ToString() + ")"; + } + } + + msg += ": " + logMsg; + return msg; } @@ -376,7 +469,7 @@ namespace GeneratorCode.Logs [CallerMemberName] string funName = "", [CallerLineNumber] int lineNo = 0) { - if (NLogLevel.Debug >= _logCfg.LogLevel) return; + if (NLogLevel.Debug >= _logCfg.LogLevel || !_logCfg.LogEnable) return; if (_logCfg.AsyncMode) { @@ -409,7 +502,7 @@ namespace GeneratorCode.Logs [CallerMemberName] string funName = "", [CallerLineNumber] int lineNo = 0) { - if (logLevel >= _logCfg.LogLevel) return; + if (logLevel >= _logCfg.LogLevel || !_logCfg.LogEnable) return; if (_logCfg.AsyncMode) { diff --git a/GeneratorCode/Program.cs b/GeneratorCode/Program.cs index c077877..e51143b 100644 --- a/GeneratorCode/Program.cs +++ b/GeneratorCode/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Text; using System.Threading; @@ -18,8 +19,8 @@ namespace GeneratorCode image_type = new[] { false, false, false, true }; StartPageID = 0; key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y"; - filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf"; - //filePath = "C:\\Works\\pdf\\123.pdf"; + //filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf"; + filePath = "C:\\Works\\pdf\\123.pdf"; sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191"; } @@ -84,6 +85,7 @@ namespace GeneratorCode public static int SendRspMessage(string msg) { + NLog.Debug("Send: " + msg + Environment.NewLine); Console.WriteLine(msg); return 0; } @@ -104,11 +106,36 @@ namespace GeneratorCode { NConfig.InitConfigure(); NLog.NLog_Init(); - //NLog myLog = new NLog(); GeneratorParams inParams = null; RspMessage rspMsg; var tmObj = new TmatrixClass(); + AppDomain.CurrentDomain.UnhandledException += (sender, e) => + { + //Trace.WriteLine("+++++++++++++++++++++++++++++++++++++++++++xajhuang1\n"); + }; + + AppDomain.CurrentDomain.DomainUnload += (sender, e) => + { + //Trace.WriteLine("+++++++++++++++++++++++++++++++++++++++++++xajhuang2\n"); + }; + + AppDomain.CurrentDomain.ProcessExit += (sender, ev) => + { + try + { + tmObj.TmatrixUninitialize(); + } + catch (Exception e) + { + //var msg = rspMsg.FormatRspMessage(9, e.Message, 0); + //RspMessage.SendRspMessage(msg); + NLog.Crash(e.ToString()); + } + + NLog.NLog_Finish(); + }; + //string inputArg = // "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" + // "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" + @@ -158,12 +185,13 @@ namespace GeneratorCode var jsInput = JsonConvert.SerializeObject(inParams); - NLog.Debug("Input:\n" + jsInput); - //Console.WriteLine("Input:\n" + Convert.ToBase64String(Encoding.Default.GetBytes(jsInput))); - - Console.Read(); - return 0; - if (!File.Exists(inParams.filePath)) return -(int) ErrCode.ERR_FILE_NOTEXISTS; + NLog.Debug("Input:" + jsInput + Environment.NewLine); + + if (!File.Exists(inParams.filePath)) + { + NLog.Error("File Not Exists: " + inParams.filePath + Environment.NewLine); + return -(int) ErrCode.ERR_FILE_NOTEXISTS; + } try { @@ -173,9 +201,10 @@ namespace GeneratorCode { var msg = rspMsg.FormatRspMessage(7, e.Message, 0); RspMessage.SendRspMessage(msg); + NLog.Crash(e.ToString()); return -(int) ErrCode.ERR_EXCEPT_THROW; } - + try { var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key, @@ -187,6 +216,7 @@ namespace GeneratorCode var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), ret.Substring(1, ret.Length - 1), tmObj.GetProgerss()); + NLog.Error(msg + Environment.NewLine); RspMessage.SendRspMessage(msg); } else @@ -194,6 +224,7 @@ namespace GeneratorCode var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), ret.Substring(1, ret.Length - 1), 100); + NLog.Debug(msg + Environment.NewLine); RspMessage.SendRspMessage(msg); } } @@ -201,20 +232,10 @@ namespace GeneratorCode { var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss()); RspMessage.SendRspMessage(msg); + NLog.Crash(e.ToString()); return -(int) ErrCode.ERR_EXCEPT_THROW; } - - try - { - tmObj.TmatrixUninitialize(); - } - catch (Exception e) - { - var msg = rspMsg.FormatRspMessage(9, e.Message, 0); - RspMessage.SendRspMessage(msg); - return -(int) ErrCode.ERR_EXCEPT_THROW; - } - + return 0; } diff --git a/GeneratorCode/config.ini b/GeneratorCode/config.ini index a7d8690..6b279c6 100644 --- a/GeneratorCode/config.ini +++ b/GeneratorCode/config.ini @@ -4,15 +4,35 @@ ; Log 相关配置 ; [LogGlobal] +; 是否启用日志功能 +LogEnable = true ; log 打印等级 LogLevel = 255 ; 默认日志打印等级 DefaultLogLevel = 4 ; 使用异步日志输出模式,默认false -AsyncMode = true +AsyncMode = false ; 是否在每条日志后加入换行 AutoForceNewLine = false +; 日志输出格式控制 +[LogFormat] +; 是否显示日期 +ShowDate = true +; 是否显示时间 +ShowTime = true +; 是否显示时间的ms精度 +ShowMSec = true +; 是否显示日志等级 +ShowLevel = true +; 是否显示源代码文件名 +ShowCodeFile = true +; 是否显示函数名 +ShowFunction = true +; 是否显示源代码行号 +ShowCodeLine = true + + ; 日志输出配置 [LogOutput] ; 是否允许控制台输出, 默认 true @@ -20,7 +40,7 @@ Console = true ; 是否允许在 Visual Studio 调试器中输出(Release 版本), 默认 false Trace = true ; 是否允许在 Visual Studio 调试器中输出(Debug 版本), 默认 false -Debug = false +Debug = true ; 是否允许在文件中输出, 默认 false File = true @@ -38,7 +58,7 @@ NumItemsOutEachTime = 10 ; 文件日志配置 [FileLogSetting] ; 日志创建目录 -Path = .\ +Path = ; 日志文件名格式 FileNamePrefix = ; 是否自动分割日志文件