69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using DTS.Common.Utilities.Logging;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using static DTS.Common.Utilities.Logging.APILogger;
|
|
|
|
namespace DbAPI.Logging
|
|
{
|
|
internal class LogManager
|
|
{
|
|
public static Sink DBAPILogWriter;
|
|
private static TextLogger _LogWriter;
|
|
private static object MyLock = new object();
|
|
private static int _MinLog = (int)(TraceEventType.Error | TraceEventType.Warning);
|
|
public enum LogEvents
|
|
{
|
|
Message,
|
|
Connections,
|
|
Login,
|
|
Information,
|
|
DataRecorders,
|
|
Graphs,
|
|
CalculatedChannels,
|
|
Sensors,
|
|
TestSetups,
|
|
RegionsOfInterest,
|
|
Tags
|
|
}
|
|
public static void Initialize(int logSize, string path, int minLog)
|
|
{
|
|
lock (MyLock)
|
|
{
|
|
if (null != _LogWriter) { return; }
|
|
_MinLog = minLog;
|
|
_LogWriter = new TextLogger(Path.Combine(path, "DB.log"), OnWriteException, logSize);
|
|
_LogWriter.ReRollLog = true;
|
|
_LogWriter.LogStartMessage = GetLogStartMessage();
|
|
DBAPILogWriter = LogMsg;
|
|
}
|
|
}
|
|
public static void Log(TraceEventType eventType, LogEvents logEvent, string msg)
|
|
{
|
|
if ((_MinLog & (int)eventType) != (int)eventType) { return; } //don't log
|
|
var dt = DateTime.Now;
|
|
LogMsg($"{dt.Year}-{dt.Month:00}-{dt.Day:00} {dt.Hour:00}:{dt.Minute:00}:{dt.Second:00}.{dt.Millisecond:000} {eventType} - {logEvent} - {msg}");
|
|
}
|
|
public static void OnWriteException(Exception ex)
|
|
{
|
|
Console.Error.WriteLine(ex.Message);
|
|
}
|
|
private static void LogMsg(string msg)
|
|
{
|
|
try
|
|
{
|
|
_LogWriter.LogMessage(msg);
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
private static string GetLogStartMessage()
|
|
{
|
|
return string.Format("DB API\r\n" +
|
|
$"OS: {Environment.OSVersion}\r\n" +
|
|
$"MachineName: {Environment.MachineName}\r\n" +
|
|
$"Environment: {Environment.Version} \r\n\n" +
|
|
"=====================================\r\n");
|
|
}
|
|
}
|
|
}
|