180 lines
5.8 KiB
C#
180 lines
5.8 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|