parent
fb9b3bfb6c
commit
c2d6186f61
|
@ -1,3 +1,4 @@
|
|||
.vs/
|
||||
/GeneratorCode/obj
|
||||
/GeneratorCode/bin/Debug
|
||||
/TmatrixCodeGenerator/
|
|
@ -0,0 +1,121 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Timers;
|
||||
using IniParser;
|
||||
using IniParser.Model;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace GeneratorCode.Configure
|
||||
{
|
||||
public static class NConfig
|
||||
{
|
||||
private static string _cfgFileName = @".\config.ini";
|
||||
|
||||
private static IniData _cfgData = null;
|
||||
|
||||
public delegate void ConfigChangedHandle();
|
||||
|
||||
public static event ConfigChangedHandle OnConfigChanged;
|
||||
|
||||
private static void ConfigChanged()
|
||||
{
|
||||
OnConfigChanged?.Invoke();
|
||||
}
|
||||
|
||||
private static void LoadCfgFromFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_cfgFileName))
|
||||
{
|
||||
var fs = new FileStream(_cfgFileName, FileMode.Create, FileAccess.Write);
|
||||
var ws = new StreamWriter(fs);
|
||||
ws.WriteLine("; 应用程序配置文件\n");
|
||||
}
|
||||
|
||||
FileIniDataParser iniPraser = new FileIniDataParser();
|
||||
iniPraser.Parser.Configuration.CommentString = ";";
|
||||
_cfgData = iniPraser.ReadFile(_cfgFileName);
|
||||
//Trace.WriteLine(_cfgData);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Trace.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void InitConfigure()
|
||||
{
|
||||
var cfgTask = new List<string>();
|
||||
var appPath = Process.GetCurrentProcess().MainModule.FileName;
|
||||
|
||||
Trace.WriteLine("Application: " + appPath);
|
||||
Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
|
||||
Trace.WriteLine("File: " + Path.GetFileName(appPath));
|
||||
|
||||
var fw = new FileSystemWatcher(@"E:\", @"my.cfg")
|
||||
{
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
|
||||
//Filter = _cfgFileName,
|
||||
//Path = Path.GetDirectoryName(appPath),
|
||||
IncludeSubdirectories = false,
|
||||
};
|
||||
|
||||
fw.Changed += (sender, e) =>
|
||||
{
|
||||
//if (Path.GetFileName(appPath) != _cfgFileName) return;
|
||||
|
||||
lock (cfgTask)
|
||||
{
|
||||
if (cfgTask.Contains(e.FullPath)) return;
|
||||
|
||||
cfgTask.Add(e.FullPath);
|
||||
}
|
||||
|
||||
var tm = new Timer(1000) { AutoReset = false };
|
||||
|
||||
tm.Elapsed += (obj, args) =>
|
||||
{
|
||||
lock (cfgTask)
|
||||
{
|
||||
cfgTask.Remove(e.FullPath);
|
||||
LoadCfgFromFile();
|
||||
ConfigChanged();
|
||||
// LogOut("File: " + e.FullPath + " ==> " + e.ChangeType.ToString() + "\n");
|
||||
}
|
||||
};
|
||||
|
||||
tm.Start();
|
||||
};
|
||||
|
||||
fw.EnableRaisingEvents = true;
|
||||
|
||||
LoadCfgFromFile();
|
||||
}
|
||||
|
||||
public static T GetCfgValue<T>([NotNull] string secName, [NotNull] string keyName, T defValue = default(T))
|
||||
{
|
||||
var ret = defValue;
|
||||
|
||||
if (_cfgData.Equals(null))
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ret = (T) Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
|
||||
ret = defValue;
|
||||
throw(e);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,9 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="INIFileParser, Version=2.5.2.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="itextsharp">
|
||||
<HintPath>Libs\itextsharp.dll</HintPath>
|
||||
</Reference>
|
||||
|
@ -53,7 +56,8 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="NLog.cs" />
|
||||
<Compile Include="Configure\NConfig.cs" />
|
||||
<Compile Include="Logs\NLog.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -75,7 +79,7 @@
|
|||
<Content Include="OIDPublishImageGenerator\OIDPatternGenerator.bin">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="log.config">
|
||||
<Content Include="config.ini">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="packages.config" />
|
||||
|
|
|
@ -0,0 +1,491 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using GeneratorCode.Configure;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace GeneratorCode.Logs
|
||||
{
|
||||
public enum NLogLevel
|
||||
{
|
||||
Fatal = 0,
|
||||
Crash,
|
||||
Error,
|
||||
Warring,
|
||||
Info,
|
||||
Debug,
|
||||
MaxLevel
|
||||
}
|
||||
|
||||
public enum CacheOptMode
|
||||
{
|
||||
Drop = 0,
|
||||
Replease
|
||||
}
|
||||
|
||||
public class NLogConfig
|
||||
{
|
||||
private object _cfgLock = new object();
|
||||
public delegate void LogCfgChangedHandle();
|
||||
|
||||
public static event LogCfgChangedHandle OnLogCfgChanged;
|
||||
|
||||
public NLogConfig()
|
||||
{
|
||||
LogConfig();
|
||||
NConfig.OnConfigChanged += () => { LogCfgChanged(); };
|
||||
}
|
||||
|
||||
protected static void LogCfgChanged()
|
||||
{
|
||||
OnLogCfgChanged?.Invoke();
|
||||
}
|
||||
|
||||
public NLogLevel LogLevel { get; set; }
|
||||
public NLogLevel DefaultLevel { get; set; }
|
||||
public bool AsyncMode { get; set; }
|
||||
public bool ForceNewLine { get; set; }
|
||||
|
||||
public bool EnConsole { get; set; }
|
||||
public bool EnTrace { get; set; }
|
||||
public bool EnDebug { get; set; }
|
||||
public bool EnFile { get; set; }
|
||||
|
||||
|
||||
public int MaxItemsCache { get; set; }
|
||||
public CacheOptMode CacheMode { get; set; }
|
||||
public int SleepTime { get; set; }
|
||||
public int NumOutItems { get; set; }
|
||||
|
||||
public string Path { get; set; }
|
||||
public string FileNamePre { get; set; }
|
||||
public bool EnSplitLog { get; set; }
|
||||
public bool SplitByData { get; set; }
|
||||
public int SplitBySize { get; set; }
|
||||
public bool RoolbackFile { get; set; }
|
||||
public int MaxFileNum { get; set; }
|
||||
|
||||
public void LogConfig()
|
||||
{
|
||||
lock (_cfgLock)
|
||||
{
|
||||
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);
|
||||
|
||||
EnConsole = NConfig.GetCfgValue("LogOutput", "Console", true);
|
||||
EnTrace = NConfig.GetCfgValue("LogOutput", "Trace", false);
|
||||
EnDebug = NConfig.GetCfgValue("LogOutput", "Debug", true);
|
||||
EnFile = NConfig.GetCfgValue("LogOutput", "File", false);
|
||||
|
||||
if (AsyncMode)
|
||||
{
|
||||
MaxItemsCache = NConfig.GetCfgValue("AsyncLogSetting", "MaxItemsCache", 0);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (EnFile)
|
||||
{
|
||||
Path = NConfig.GetCfgValue("FileLogSetting", "Path", @"./");
|
||||
FileNamePre = NConfig.GetCfgValue("FileLogSetting", "FileNamePrefix", "");
|
||||
EnSplitLog = NConfig.GetCfgValue("FileLogSetting", "AutoSplitFile", true);
|
||||
|
||||
if (EnSplitLog)
|
||||
{
|
||||
SplitByData = NConfig.GetCfgValue("SplitFiles", "SplitByDate", true);
|
||||
SplitBySize = NConfig.GetCfgValue("SplitFiles", "SplitBySize", 4);
|
||||
RoolbackFile = NConfig.GetCfgValue("SplitFiles", "FileNameRollback", true);
|
||||
MaxFileNum = NConfig.GetCfgValue("SplitFiles", "MaxFileNameNum", 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NLogItem
|
||||
{
|
||||
public NLogItem(NLogLevel level = NLogLevel.Debug,
|
||||
[NotNull] string logContent = "",
|
||||
[NotNull] string fileName = "",
|
||||
[NotNull] string funName = "",
|
||||
int lineNo = 0,
|
||||
DateTime? dt = null)
|
||||
{
|
||||
if (dt == null)
|
||||
{
|
||||
LogStamp = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
LogStamp = (DateTime)dt;
|
||||
}
|
||||
|
||||
LogLevel = level;
|
||||
LogContent = logContent;
|
||||
CodeFile = fileName;
|
||||
CodeFunction = funName;
|
||||
CodeLine = lineNo;
|
||||
}
|
||||
|
||||
public DateTime LogStamp { get; set; }
|
||||
public NLogLevel LogLevel { get; set; }
|
||||
public string LogContent { get; set; }
|
||||
public string CodeFile { get; set; }
|
||||
public int CodeLine { get; set; }
|
||||
public string CodeFunction { get; set; }
|
||||
}
|
||||
|
||||
public static class NLog
|
||||
{
|
||||
private static NLogConfig _logCfg;
|
||||
private static readonly ConcurrentQueue<NLogItem> _logItemCollection = new ConcurrentQueue<NLogItem>();
|
||||
private static readonly object _logOutputLock = new object();
|
||||
private static string _logFileName = "";
|
||||
private static uint _logFileNum = 0;
|
||||
private static StreamWriter _logSw = null;
|
||||
|
||||
private static void CreateLogFileHead()
|
||||
{
|
||||
_logSw?.WriteLine("FileName: " + _logFileName);
|
||||
_logSw?.WriteLine("CreateTime: " + string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
|
||||
_logSw?.WriteLine("Program: " + Process.GetCurrentProcess().MainModule.FileName);
|
||||
_logSw?.WriteLine("PID: " + Process.GetCurrentProcess().Id);
|
||||
_logSw?.WriteLine("Split Count: " + (_logFileNum - 1).ToString());
|
||||
_logSw?.WriteLine("--------------------------------------------------");
|
||||
_logSw?.Flush();
|
||||
}
|
||||
|
||||
private static void ConfigInit()
|
||||
{
|
||||
if (_logCfg.EnFile)
|
||||
{
|
||||
_logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}",
|
||||
_logCfg.Path,
|
||||
_logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "",
|
||||
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName),
|
||||
DateTime.Now,
|
||||
Process.GetCurrentProcess().Id);
|
||||
|
||||
if (_logCfg.EnSplitLog && _logCfg.RoolbackFile && _logCfg.MaxFileNum > 0)
|
||||
{
|
||||
_logFileName += $"_{_logFileNum:d3}";
|
||||
_logFileNum = Convert.ToUInt32((_logFileNum + 1) % _logCfg.MaxFileNum);
|
||||
}
|
||||
|
||||
_logFileName += ".log";
|
||||
|
||||
if (File.Exists(_logFileName))
|
||||
{
|
||||
if (_logCfg.EnSplitLog)
|
||||
{
|
||||
File.Delete(_logFileName);
|
||||
}
|
||||
}
|
||||
|
||||
_logSw = new StreamWriter(_logFileName, true);
|
||||
CreateLogFileHead();
|
||||
}
|
||||
}
|
||||
|
||||
public static void NLog_Init()
|
||||
{
|
||||
_logCfg = new NLogConfig();
|
||||
|
||||
NLogConfig.OnLogCfgChanged += () =>
|
||||
{
|
||||
_logSw?.Close();
|
||||
|
||||
_logCfg.LogConfig();
|
||||
|
||||
ConfigInit();
|
||||
};
|
||||
|
||||
ConfigInit();
|
||||
|
||||
var asynWork = new Thread(() =>
|
||||
{
|
||||
uint cnt = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var lastDt = DateTime.Now;
|
||||
var tolOut = Math.Min(_logCfg.NumOutItems, _logItemCollection.Count);
|
||||
|
||||
foreach (var val in Enumerable.Range(1, tolOut))
|
||||
{
|
||||
if (_logItemCollection.TryDequeue(out var logItem))
|
||||
{
|
||||
LogOutput(logItem);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(_logCfg.SleepTime);
|
||||
|
||||
// 每秒执行一次维护工作
|
||||
if ((++cnt % (1000 / _logCfg.SleepTime)) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_logSw?.Flush();
|
||||
|
||||
if (_logCfg.EnSplitLog)
|
||||
{
|
||||
bool isNeedSplit = true;
|
||||
|
||||
if (_logCfg.SplitByData && lastDt.Day != DateTime.Now.Day)
|
||||
{
|
||||
isNeedSplit = true;
|
||||
}
|
||||
else if (_logCfg.SplitBySize > 0 && (new FileInfo(_logFileName)).Length > _logCfg.SplitBySize * (1024 * 1024))
|
||||
{
|
||||
isNeedSplit = true;
|
||||
}
|
||||
|
||||
if (isNeedSplit)
|
||||
{
|
||||
_logSw?.Close();
|
||||
_logSw?.Dispose();
|
||||
_logSw = null;
|
||||
|
||||
string parttn = string.Format("*[{0:d3}]_{1}",
|
||||
Process.GetCurrentProcess().Id,
|
||||
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName));
|
||||
|
||||
_logFileName = string.Format("{0}{1}[{3:yyyy-MM-dd_HH-mm}][{4:d}]_{2}",
|
||||
_logCfg.Path,
|
||||
_logCfg.FileNamePre.Length > 0 ? _logCfg.FileNamePre + "_" : "",
|
||||
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName),
|
||||
DateTime.Now,
|
||||
Process.GetCurrentProcess().Id);
|
||||
|
||||
if (_logCfg.EnSplitLog && _logCfg.RoolbackFile && _logCfg.MaxFileNum > 0)
|
||||
{
|
||||
_logFileName += $"_{_logFileNum:d3}";
|
||||
parttn += $"_{_logFileNum:d3}";
|
||||
_logFileNum = Convert.ToUInt32((_logFileNum + 1) % _logCfg.MaxFileNum);
|
||||
}
|
||||
|
||||
_logFileName += ".log";
|
||||
parttn += ".log";
|
||||
|
||||
foreach (var f in Directory.GetFiles(_logCfg.Path, parttn, SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
File.Delete(f);
|
||||
Trace.WriteLine("Delect Rollback log: " + f);
|
||||
}
|
||||
|
||||
_logSw = new StreamWriter(_logFileName, true);
|
||||
CreateLogFileHead();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
{
|
||||
Name = "Log Async Output Thread",
|
||||
IsBackground = true
|
||||
};
|
||||
|
||||
asynWork.Start();
|
||||
}
|
||||
|
||||
private static string LogLevelToString([NotNull] NLogLevel logLevel)
|
||||
{
|
||||
string[] level = { "F", "C", "E", "I", "W", "D" };
|
||||
|
||||
if ((int) logLevel < level.Length && (int) logLevel >= 0)
|
||||
return level[(int) logLevel];
|
||||
return "U";
|
||||
}
|
||||
|
||||
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;
|
||||
return msg;
|
||||
}
|
||||
|
||||
private static void LogOutput(NLogItem logItem)
|
||||
{
|
||||
var msg = LogFormat(logItem.LogLevel,
|
||||
logItem.LogContent,
|
||||
logItem.CodeFile,
|
||||
logItem.CodeFunction,
|
||||
logItem.CodeLine,
|
||||
logItem.LogStamp);
|
||||
|
||||
if (_logCfg.ForceNewLine)
|
||||
{
|
||||
msg += Environment.NewLine;
|
||||
}
|
||||
|
||||
lock (_logOutputLock)
|
||||
{
|
||||
if (_logCfg.EnConsole) Console.Write(msg);
|
||||
|
||||
if (_logCfg.EnDebug | _logCfg.EnTrace)
|
||||
{
|
||||
if (_logCfg.EnTrace)
|
||||
Trace.Write(msg);
|
||||
else
|
||||
System.Diagnostics.Debug.WriteLine(msg);
|
||||
}
|
||||
|
||||
if (_logCfg.EnFile)
|
||||
{
|
||||
_logSw?.Write(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void LogOutput2(string logMsg)
|
||||
{
|
||||
lock (_logOutputLock)
|
||||
{
|
||||
if (_logCfg.EnConsole) Console.Write(logMsg);
|
||||
|
||||
if (_logCfg.EnDebug | _logCfg.EnTrace)
|
||||
{
|
||||
if (_logCfg.EnTrace)
|
||||
Trace.Write(logMsg);
|
||||
else
|
||||
System.Diagnostics.Debug.WriteLine(logMsg);
|
||||
}
|
||||
|
||||
if (_logCfg.EnFile)
|
||||
{
|
||||
_logSw?.Write(logMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void LogOut([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
if (NLogLevel.Debug >= _logCfg.LogLevel) return;
|
||||
|
||||
if (_logCfg.AsyncMode)
|
||||
{
|
||||
if (_logItemCollection.Count >= _logCfg.MaxItemsCache)
|
||||
{
|
||||
if (_logCfg.CacheMode == CacheOptMode.Drop)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
NLogItem val;
|
||||
_logItemCollection.TryDequeue(out val);
|
||||
}
|
||||
}
|
||||
|
||||
var logItem = new NLogItem(_logCfg.DefaultLevel, logMsg, fileName, funName, lineNo, DateTime.Now);
|
||||
_logItemCollection.Enqueue(logItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
var logItem = new NLogItem(_logCfg.DefaultLevel, logMsg, fileName, funName, lineNo, DateTime.Now);
|
||||
LogOutput(logItem);
|
||||
}
|
||||
}
|
||||
|
||||
public static void LogOut(NLogLevel logLevel = NLogLevel.Info,
|
||||
[NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
if (logLevel >= _logCfg.LogLevel) return;
|
||||
|
||||
if (_logCfg.AsyncMode)
|
||||
{
|
||||
if (_logItemCollection.Count >= _logCfg.MaxItemsCache)
|
||||
{
|
||||
if (_logCfg.CacheMode == CacheOptMode.Drop)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
NLogItem val;
|
||||
_logItemCollection.TryDequeue(out val);
|
||||
}
|
||||
}
|
||||
|
||||
var logItem = new NLogItem(NLogLevel.Debug, logMsg, fileName, funName, lineNo, DateTime.Now);
|
||||
_logItemCollection.Enqueue(logItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
var logItem = new NLogItem(NLogLevel.Debug, logMsg, fileName, funName, lineNo, DateTime.Now);
|
||||
LogOutput(logItem);
|
||||
}
|
||||
}
|
||||
|
||||
#region LogOutputMethod
|
||||
|
||||
public static void Debug([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Debug, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public static void Warring([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Warring, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public static void Info([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Info, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public static void Error([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Error, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public static void Crash([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Crash, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public static void Fatal([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Fatal, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -1,179 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Timers;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace GeneratorCode
|
||||
{
|
||||
public enum NLogLevel
|
||||
{
|
||||
Fatal = 0,
|
||||
Error,
|
||||
Info,
|
||||
Warring,
|
||||
Debug
|
||||
}
|
||||
|
||||
public class LogConfigInfo : ConfigurationSection
|
||||
{
|
||||
[ConfigurationProperty("logLevel")] public int logLevel => int.Parse(string.Format("{0}", base["logLevel"]));
|
||||
}
|
||||
|
||||
public class NLog
|
||||
{
|
||||
public NLog()
|
||||
{
|
||||
var cfgTask = new List<string>();
|
||||
|
||||
var fw = new FileSystemWatcher
|
||||
{
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
|
||||
Filter = "my.cfg",
|
||||
IncludeSubdirectories = false,
|
||||
Path = @"E:\"
|
||||
};
|
||||
|
||||
fw.Changed += (sender, e) =>
|
||||
{
|
||||
lock (cfgTask)
|
||||
{
|
||||
if (cfgTask.Contains(e.FullPath)) return;
|
||||
|
||||
cfgTask.Add(e.FullPath);
|
||||
}
|
||||
|
||||
var tm = new Timer(1000) { AutoReset = false };
|
||||
|
||||
tm.Elapsed += (obj, args) =>
|
||||
{
|
||||
lock (cfgTask)
|
||||
{
|
||||
cfgTask.Remove(e.FullPath);
|
||||
LogOut("File: " + e.FullPath + " ==> " + e.ChangeType.ToString() + "\n");
|
||||
var cfgMap = new ExeConfigurationFileMap
|
||||
{
|
||||
ExeConfigFilename = @".\log.config"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var v = ConfigurationManager.OpenMappedExeConfiguration(cfgMap,
|
||||
ConfigurationUserLevel.None);
|
||||
|
||||
var cfgGrp = v.GetSection("global") as LogConfigInfo;
|
||||
|
||||
Trace.WriteLine("LogLevel: [" + cfgGrp.logLevel.ToString() + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Trace.WriteLine(exception.Message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
tm.Start();
|
||||
};
|
||||
|
||||
//FileSystemWatcher fw = new FileSystemWatcher(@"E:\", "*.cfg");
|
||||
|
||||
//fw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName;
|
||||
;
|
||||
//fw.Changed += new FileSystemEventHandler(OnFileChanged);
|
||||
//fw.Created += new FileSystemEventHandler(OnFileChanged);
|
||||
//fw.Deleted += new FileSystemEventHandler(OnFileChanged);
|
||||
|
||||
fw.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
private string LogLevelToString([NotNull] NLogLevel logLevel)
|
||||
{
|
||||
string[] level = { "F", "E", "I", "W", "D" };
|
||||
|
||||
if ((int) logLevel < level.Length && (int) logLevel >= 0)
|
||||
return level[(int) logLevel];
|
||||
return "U";
|
||||
}
|
||||
|
||||
private string LogFormat(NLogLevel logLevel, string logMsg, string fileName, string funName, int lineNo)
|
||||
{
|
||||
var msg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
|
||||
+ "] [" + LogLevelToString(logLevel) + "] [" + Path.GetFileName(fileName) + "] - "
|
||||
+ funName + "(" + lineNo + "):" + logMsg;
|
||||
return msg;
|
||||
}
|
||||
|
||||
private void LogOutput(string logMsg)
|
||||
{
|
||||
var thisLock = new object();
|
||||
|
||||
lock (thisLock)
|
||||
{
|
||||
Console.Write(logMsg);
|
||||
Trace.Write(logMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public void LogOut([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
var msg = LogFormat(NLogLevel.Debug, logMsg, fileName, funName, lineNo);
|
||||
LogOutput(msg);
|
||||
}
|
||||
|
||||
public void LogOut(NLogLevel logLevel = NLogLevel.Info,
|
||||
[NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
var msg = LogFormat(logLevel, logMsg, fileName, funName, lineNo);
|
||||
LogOutput(msg);
|
||||
}
|
||||
|
||||
public void Debug([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Debug, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public void Warring([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Warring, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public void Info([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Info, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public void Error([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Error, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
|
||||
public void Fatal([NotNull] string logMsg = "",
|
||||
[CallerFilePath] string fileName = "",
|
||||
[CallerMemberName] string funName = "",
|
||||
[CallerLineNumber] int lineNo = 0)
|
||||
{
|
||||
LogOut(NLogLevel.Fatal, logMsg, fileName, funName, lineNo);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using GeneratorCode.Configure;
|
||||
using GeneratorCode.Logs;
|
||||
using Newtonsoft.Json;
|
||||
using TmatrixLibrary;
|
||||
|
||||
|
@ -15,8 +18,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";
|
||||
}
|
||||
|
||||
|
@ -99,7 +102,10 @@ namespace GeneratorCode
|
|||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
GeneratorParams inParams;
|
||||
NConfig.InitConfigure();
|
||||
NLog.NLog_Init();
|
||||
//NLog myLog = new NLog();
|
||||
GeneratorParams inParams = null;
|
||||
RspMessage rspMsg;
|
||||
var tmObj = new TmatrixClass();
|
||||
|
||||
|
@ -116,7 +122,7 @@ namespace GeneratorCode
|
|||
// "ZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsImZpbGVQYXRoIjoiQzpc" +
|
||||
// "XFdvcmtzXFxwZGZcXDEyMy5wZGYiLCJTdGFydFBhZ2VJRCI6MCwic2Vzc" +
|
||||
// "2lvbklkIjoiNEJENUQ5MjMtNDdFQS00REVGLUExQ0QtOUI4NUI1MTVCMTkxIn0=";
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
if (args.Length == 1)
|
||||
|
@ -142,6 +148,7 @@ namespace GeneratorCode
|
|||
rspMsg = new RspMessage("");
|
||||
var msg = rspMsg.FormatRspMessage(10, e.Message, 0);
|
||||
RspMessage.SendRspMessage(msg);
|
||||
NLog.Crash(string.Format("[{0}]: ", (inParams == null) ? inParams.sessionId : "UnInit") + e.Message);
|
||||
return -(int) ErrCode.ERR_JSON_DECODE;
|
||||
}
|
||||
|
||||
|
@ -151,9 +158,11 @@ namespace GeneratorCode
|
|||
|
||||
var jsInput = JsonConvert.SerializeObject(inParams);
|
||||
|
||||
Console.WriteLine("Input:\n" + jsInput);
|
||||
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;
|
||||
|
||||
try
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
; 应用程序配置文件
|
||||
|
||||
;---------------------------------------
|
||||
; Log 相关配置
|
||||
;
|
||||
[LogGlobal]
|
||||
; log 打印等级
|
||||
LogLevel = 255
|
||||
; 默认日志打印等级
|
||||
DefaultLogLevel = 4
|
||||
; 使用异步日志输出模式,默认false
|
||||
AsyncMode = true
|
||||
; 是否在每条日志后加入换行
|
||||
AutoForceNewLine = false
|
||||
|
||||
; 日志输出配置
|
||||
[LogOutput]
|
||||
; 是否允许控制台输出, 默认 true
|
||||
Console = true
|
||||
; 是否允许在 Visual Studio 调试器中输出(Release 版本), 默认 false
|
||||
Trace = true
|
||||
; 是否允许在 Visual Studio 调试器中输出(Debug 版本), 默认 false
|
||||
Debug = false
|
||||
; 是否允许在文件中输出, 默认 false
|
||||
File = true
|
||||
|
||||
; 异步输出模式配置
|
||||
[AsyncLogSetting]
|
||||
; 日志最大缓冲条数, 0:无限制
|
||||
MaxItemsCache = 1000
|
||||
; 日志缓存满后处理方法:0:丢弃, 1:覆盖最早一条
|
||||
CacheFullOpts = 0
|
||||
; 线程每次输出完成后休眠时间(毫秒)
|
||||
ThreadSleep = 10
|
||||
; 线程每次最多处理日志条数
|
||||
NumItemsOutEachTime = 10
|
||||
|
||||
; 文件日志配置
|
||||
[FileLogSetting]
|
||||
; 日志创建目录
|
||||
Path = .\
|
||||
; 日志文件名格式
|
||||
FileNamePrefix =
|
||||
; 是否自动分割日志文件
|
||||
AutoSplitFile = true
|
||||
|
||||
; 日志文件分割配置
|
||||
[SplitFiles]
|
||||
; 是否根据日期改变自动创建新的日志文件
|
||||
SplitByDate = true
|
||||
; 是否根据日志大小自动创建日志文件:
|
||||
; 0:不需要, 大于0:日志大于(字节)后自动创建新文件
|
||||
SplitBySize = 4
|
||||
; 是否启动日志文件名自动回滚功能
|
||||
FileNameRollback = true
|
||||
; 日志文件名回滚最大文件数
|
||||
MaxFileNameNum = 10
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name ="global" type="GeneratorCode.LogConfigInfo,GeneratorCode"/>
|
||||
</configSections>
|
||||
<global logLevel="255" />
|
||||
</configuration>
|
|
@ -1,4 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="ini-parser" version="2.5.2" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net45" />
|
||||
</packages>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue