SafeDispatch/TCPClientService/Utils.cs

42 lines
1.2 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Diagnostics;
namespace WindowsService1
{
public class Utils
{
private static readonly object locker = new object();
public static bool TimeWithMs = false;
public static void WriteEventLog(String source, String content, EventLogEntryType type, Int32 eventId)
{
try
{
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(source, "Application");
EventLog.WriteEntry(source, content, type, eventId);
}
catch (Exception ex)
{
Utils.WriteLine("Could not write to event log :" + ex.ToString(), ConsoleColor.Red);
}
}
public static void WriteLine(string str, ConsoleColor color)
{
lock (locker)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(String.Format("[### {0:H:mm:ss" + (TimeWithMs? ".ffffff" : "") + "} ###] ", DateTime.Now));
Console.ForegroundColor = color;
Console.Write(String.Format("{0}\n", str));
Console.ForegroundColor = ConsoleColor.Gray;
}
}
}
}