修正日志文件路径使用相对路径是系统异常问题

This commit is contained in:
hzhuangxin01 2019-02-18 19:21:24 +08:00
parent c2d6186f61
commit e34633edea
7 changed files with 181 additions and 46 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
.vs/ .vs/
/GeneratorCode/obj /GeneratorCode/obj
/GeneratorCode/bin/Debug /GeneratorCode/bin/Debug
/TmatrixCodeGenerator/ /TmatrixCodeGenerator/
/GeneratorCode/bin/Release

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup> </startup>
</configuration> </configuration>

View File

@ -51,15 +51,15 @@ namespace GeneratorCode.Configure
var cfgTask = new List<string>(); var cfgTask = new List<string>();
var appPath = Process.GetCurrentProcess().MainModule.FileName; var appPath = Process.GetCurrentProcess().MainModule.FileName;
Trace.WriteLine("Application: " + appPath); //Trace.WriteLine("Application: " + appPath);
Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath)); //Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
Trace.WriteLine("File: " + Path.GetFileName(appPath)); //Trace.WriteLine("File: " + Path.GetFileName(appPath));
var fw = new FileSystemWatcher(@"E:\", @"my.cfg") var fw = new FileSystemWatcher()
{ {
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite, NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
//Filter = _cfgFileName, Filter = _cfgFileName,
//Path = Path.GetDirectoryName(appPath), Path = Path.GetDirectoryName(appPath),
IncludeSubdirectories = false, IncludeSubdirectories = false,
}; };

View File

@ -8,7 +8,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>GeneratorCode</RootNamespace> <RootNamespace>GeneratorCode</RootNamespace>
<AssemblyName>GeneratorCode</AssemblyName> <AssemblyName>GeneratorCode</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>

View File

@ -45,11 +45,20 @@ namespace GeneratorCode.Logs
OnLogCfgChanged?.Invoke(); OnLogCfgChanged?.Invoke();
} }
public bool LogEnable { get; set; }
public NLogLevel LogLevel { get; set; } public NLogLevel LogLevel { get; set; }
public NLogLevel DefaultLevel { get; set; } public NLogLevel DefaultLevel { get; set; }
public bool AsyncMode { get; set; } public bool AsyncMode { get; set; }
public bool ForceNewLine { 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 EnConsole { get; set; }
public bool EnTrace { get; set; } public bool EnTrace { get; set; }
public bool EnDebug { get; set; } public bool EnDebug { get; set; }
@ -61,7 +70,7 @@ namespace GeneratorCode.Logs
public int SleepTime { get; set; } public int SleepTime { get; set; }
public int NumOutItems { get; set; } public int NumOutItems { get; set; }
public string Path { get; set; } public string DirPath { get; set; }
public string FileNamePre { get; set; } public string FileNamePre { get; set; }
public bool EnSplitLog { get; set; } public bool EnSplitLog { get; set; }
public bool SplitByData { get; set; } public bool SplitByData { get; set; }
@ -73,11 +82,20 @@ namespace GeneratorCode.Logs
{ {
lock (_cfgLock) lock (_cfgLock)
{ {
LogEnable = NConfig.GetCfgValue("LogGlobal", "LogEnable", false);
LogLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "LogLevel", (int)NLogLevel.MaxLevel); LogLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "LogLevel", (int)NLogLevel.MaxLevel);
DefaultLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "DefaultLogLevel", (int)NLogLevel.Info); DefaultLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "DefaultLogLevel", (int)NLogLevel.Info);
AsyncMode = NConfig.GetCfgValue("LogGlobal", "AsyncMode", false); AsyncMode = NConfig.GetCfgValue("LogGlobal", "AsyncMode", false);
ForceNewLine = NConfig.GetCfgValue("LogGlobal", "AutoForceNewLine", 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); EnConsole = NConfig.GetCfgValue("LogOutput", "Console", true);
EnTrace = NConfig.GetCfgValue("LogOutput", "Trace", false); EnTrace = NConfig.GetCfgValue("LogOutput", "Trace", false);
EnDebug = NConfig.GetCfgValue("LogOutput", "Debug", true); EnDebug = NConfig.GetCfgValue("LogOutput", "Debug", true);
@ -90,13 +108,25 @@ namespace GeneratorCode.Logs
if (MaxItemsCache == 0) MaxItemsCache = int.MaxValue; if (MaxItemsCache == 0) MaxItemsCache = int.MaxValue;
CacheMode = (CacheOptMode)NConfig.GetCfgValue("AsyncLogSetting", "CacheFullOpts", (int)CacheOptMode.Drop); CacheMode = (CacheOptMode)NConfig.GetCfgValue("AsyncLogSetting", "CacheFullOpts", (int)CacheOptMode.Drop);
SleepTime = NConfig.GetCfgValue("AsyncLogSetting", "ThreadSleep", 10);
NumOutItems = NConfig.GetCfgValue("AsyncLogSetting", "NumItemsOutEachTime", 10); NumOutItems = NConfig.GetCfgValue("AsyncLogSetting", "NumItemsOutEachTime", 10);
} }
SleepTime = NConfig.GetCfgValue("AsyncLogSetting", "ThreadSleep", 10);
if (EnFile) 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", ""); FileNamePre = NConfig.GetCfgValue("FileLogSetting", "FileNamePrefix", "");
EnSplitLog = NConfig.GetCfgValue("FileLogSetting", "AutoSplitFile", true); EnSplitLog = NConfig.GetCfgValue("FileLogSetting", "AutoSplitFile", true);
@ -170,7 +200,7 @@ namespace GeneratorCode.Logs
if (_logCfg.EnFile) if (_logCfg.EnFile)
{ {
_logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}", _logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}",
_logCfg.Path, _logCfg.DirPath,
_logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "", _logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "",
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName), Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName),
DateTime.Now, 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() public static void NLog_Init()
{ {
_logCfg = new NLogConfig(); _logCfg = new NLogConfig();
@ -241,7 +280,7 @@ namespace GeneratorCode.Logs
if (_logCfg.EnSplitLog) if (_logCfg.EnSplitLog)
{ {
bool isNeedSplit = true; bool isNeedSplit = false;
if (_logCfg.SplitByData && lastDt.Day != DateTime.Now.Day) if (_logCfg.SplitByData && lastDt.Day != DateTime.Now.Day)
{ {
@ -263,7 +302,7 @@ namespace GeneratorCode.Logs
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName)); Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName));
_logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}", _logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}",
_logCfg.Path, _logCfg.DirPath,
_logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "", _logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "",
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName), Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName),
DateTime.Now, DateTime.Now,
@ -279,7 +318,7 @@ namespace GeneratorCode.Logs
_logFileName += ".log"; _logFileName += ".log";
parttn += ".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); File.Delete(f);
Trace.WriteLine("Delect Rollback log: " + 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, private static string LogFormat(NLogLevel logLevel, string logMsg, string fileName, string funName, int lineNo,
DateTime dt) DateTime dt)
{ {
var msg = "[" + dt.ToString("yyyy-MM-dd HH:mm:ss.fff") var msg = "";
+ "] [" + LogLevelToString(logLevel) + "] [" + Path.GetFileName(fileName) + "] - "
+ funName + "(" + lineNo + "):" + logMsg; 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; return msg;
} }
@ -376,7 +469,7 @@ namespace GeneratorCode.Logs
[CallerMemberName] string funName = "", [CallerMemberName] string funName = "",
[CallerLineNumber] int lineNo = 0) [CallerLineNumber] int lineNo = 0)
{ {
if (NLogLevel.Debug >= _logCfg.LogLevel) return; if (NLogLevel.Debug >= _logCfg.LogLevel || !_logCfg.LogEnable) return;
if (_logCfg.AsyncMode) if (_logCfg.AsyncMode)
{ {
@ -409,7 +502,7 @@ namespace GeneratorCode.Logs
[CallerMemberName] string funName = "", [CallerMemberName] string funName = "",
[CallerLineNumber] int lineNo = 0) [CallerLineNumber] int lineNo = 0)
{ {
if (logLevel >= _logCfg.LogLevel) return; if (logLevel >= _logCfg.LogLevel || !_logCfg.LogEnable) return;
if (_logCfg.AsyncMode) if (_logCfg.AsyncMode)
{ {

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -18,8 +19,8 @@ namespace GeneratorCode
image_type = new[] { false, false, false, true }; image_type = new[] { false, false, false, true };
StartPageID = 0; StartPageID = 0;
key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y"; key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y";
filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf"; //filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf";
//filePath = "C:\\Works\\pdf\\123.pdf"; filePath = "C:\\Works\\pdf\\123.pdf";
sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191"; sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191";
} }
@ -84,6 +85,7 @@ namespace GeneratorCode
public static int SendRspMessage(string msg) public static int SendRspMessage(string msg)
{ {
NLog.Debug("Send: " + msg + Environment.NewLine);
Console.WriteLine(msg); Console.WriteLine(msg);
return 0; return 0;
} }
@ -104,11 +106,36 @@ namespace GeneratorCode
{ {
NConfig.InitConfigure(); NConfig.InitConfigure();
NLog.NLog_Init(); NLog.NLog_Init();
//NLog myLog = new NLog();
GeneratorParams inParams = null; GeneratorParams inParams = null;
RspMessage rspMsg; RspMessage rspMsg;
var tmObj = new TmatrixClass(); 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 = //string inputArg =
// "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" + // "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" +
// "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" + // "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" +
@ -158,12 +185,13 @@ namespace GeneratorCode
var jsInput = JsonConvert.SerializeObject(inParams); var jsInput = JsonConvert.SerializeObject(inParams);
NLog.Debug("Input:\n" + jsInput); NLog.Debug("Input:" + jsInput + Environment.NewLine);
//Console.WriteLine("Input:\n" + Convert.ToBase64String(Encoding.Default.GetBytes(jsInput)));
if (!File.Exists(inParams.filePath))
Console.Read(); {
return 0; NLog.Error("File Not Exists: " + inParams.filePath + Environment.NewLine);
if (!File.Exists(inParams.filePath)) return -(int) ErrCode.ERR_FILE_NOTEXISTS; return -(int) ErrCode.ERR_FILE_NOTEXISTS;
}
try try
{ {
@ -173,9 +201,10 @@ namespace GeneratorCode
{ {
var msg = rspMsg.FormatRspMessage(7, e.Message, 0); var msg = rspMsg.FormatRspMessage(7, e.Message, 0);
RspMessage.SendRspMessage(msg); RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW; return -(int) ErrCode.ERR_EXCEPT_THROW;
} }
try try
{ {
var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key, var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key,
@ -187,6 +216,7 @@ namespace GeneratorCode
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1), tmObj.GetProgerss()); ret.Substring(1, ret.Length - 1), tmObj.GetProgerss());
NLog.Error(msg + Environment.NewLine);
RspMessage.SendRspMessage(msg); RspMessage.SendRspMessage(msg);
} }
else else
@ -194,6 +224,7 @@ namespace GeneratorCode
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1), 100); ret.Substring(1, ret.Length - 1), 100);
NLog.Debug(msg + Environment.NewLine);
RspMessage.SendRspMessage(msg); RspMessage.SendRspMessage(msg);
} }
} }
@ -201,20 +232,10 @@ namespace GeneratorCode
{ {
var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss()); var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss());
RspMessage.SendRspMessage(msg); RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW; 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; return 0;
} }

View File

@ -4,15 +4,35 @@
; Log 相关配置 ; Log 相关配置
; ;
[LogGlobal] [LogGlobal]
; 是否启用日志功能
LogEnable = true
; log 打印等级 ; log 打印等级
LogLevel = 255 LogLevel = 255
; 默认日志打印等级 ; 默认日志打印等级
DefaultLogLevel = 4 DefaultLogLevel = 4
; 使用异步日志输出模式默认false ; 使用异步日志输出模式默认false
AsyncMode = true AsyncMode = false
; 是否在每条日志后加入换行 ; 是否在每条日志后加入换行
AutoForceNewLine = false AutoForceNewLine = false
; 日志输出格式控制
[LogFormat]
; 是否显示日期
ShowDate = true
; 是否显示时间
ShowTime = true
; 是否显示时间的ms精度
ShowMSec = true
; 是否显示日志等级
ShowLevel = true
; 是否显示源代码文件名
ShowCodeFile = true
; 是否显示函数名
ShowFunction = true
; 是否显示源代码行号
ShowCodeLine = true
; 日志输出配置 ; 日志输出配置
[LogOutput] [LogOutput]
; 是否允许控制台输出, 默认 true ; 是否允许控制台输出, 默认 true
@ -20,7 +40,7 @@ Console = true
; 是否允许在 Visual Studio 调试器中输出(Release 版本), 默认 false ; 是否允许在 Visual Studio 调试器中输出(Release 版本), 默认 false
Trace = true Trace = true
; 是否允许在 Visual Studio 调试器中输出(Debug 版本), 默认 false ; 是否允许在 Visual Studio 调试器中输出(Debug 版本), 默认 false
Debug = false Debug = true
; 是否允许在文件中输出, 默认 false ; 是否允许在文件中输出, 默认 false
File = true File = true
@ -38,7 +58,7 @@ NumItemsOutEachTime = 10
; 文件日志配置 ; 文件日志配置
[FileLogSetting] [FileLogSetting]
; 日志创建目录 ; 日志创建目录
Path = .\ Path =
; 日志文件名格式 ; 日志文件名格式
FileNamePrefix = FileNamePrefix =
; 是否自动分割日志文件 ; 是否自动分割日志文件