1. 增加NotNull等自定义属性定义配置文件。

2. 增加log功能
This commit is contained in:
hzhuangxin01 2019-02-14 15:17:51 +08:00
parent 106befe42e
commit efac538006
5 changed files with 1259 additions and 5 deletions

7
.gitignore vendored
View File

@ -1,6 +1,3 @@
.vs/
./GeneratorCode/bin/
./GeneratorCode/obj/
./TmatrixCodeGenerator/bin/
./TmatrixCodeGenerator/obj/
/GeneratorCode/obj
/GeneratorCode/bin/Debug

View File

@ -39,6 +39,7 @@
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
@ -50,7 +51,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NLog.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TmatrixSDK\OIDPublishImageGenerator.cs" />
<Compile Include="TmatrixSDK\TmatrixClass.cs" />
@ -69,6 +72,9 @@
<Content Include="OIDPublishImageGenerator\OIDPatternGenerator.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="log.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Libs\" />

179
GeneratorCode/NLog.cs Normal file
View File

@ -0,0 +1,179 @@
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);
}
}
}

File diff suppressed because it is too large Load Diff

7
GeneratorCode/log.config Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="global" type="GeneratorCode.LogConfigInfo,GeneratorCode"/>
</configSections>
<global logLevel="255" />
</configuration>