naps2/NAPS2.Lib.WinForms/Logging/WindowsEventLogger.cs

35 lines
905 B
C#
Raw Permalink Normal View History

namespace NAPS2.Logging;
2021-12-17 22:59:54 +03:00
public class WindowsEventLogger : IEventLogger
2018-09-21 19:08:50 +03:00
{
2021-12-17 22:59:54 +03:00
private const string SOURCE_NAME = "NAPS2";
private const string LOG_NAME = "Application";
private readonly Naps2Config _config;
2019-03-27 05:31:25 +03:00
public WindowsEventLogger(Naps2Config config)
2021-12-17 22:59:54 +03:00
{
_config = config;
}
2019-03-27 05:31:25 +03:00
2021-12-17 22:59:54 +03:00
public void CreateEventSource()
{
if (!EventLog.SourceExists(SOURCE_NAME))
2019-03-27 05:31:25 +03:00
{
2021-12-17 22:59:54 +03:00
EventLog.CreateEventSource(SOURCE_NAME, LOG_NAME);
2019-03-27 05:31:25 +03:00
}
2021-12-17 22:59:54 +03:00
}
2019-03-27 05:31:25 +03:00
2021-12-17 22:59:54 +03:00
public void LogEvent(EventType eventType, EventParams eventParams)
{
if (!_config.Get(c => c.EventLogging).HasFlag(eventType)) return;
try
2018-09-21 19:08:50 +03:00
{
2021-12-17 22:59:54 +03:00
EventLog.WriteEntry(SOURCE_NAME, eventParams.ToString(), EventLogEntryType.Information);
2018-09-21 19:08:50 +03:00
}
2021-12-17 22:59:54 +03:00
catch (Exception ex)
2018-09-21 19:08:50 +03:00
{
2021-12-17 22:59:54 +03:00
Log.ErrorException("Error writing to windows event log", ex);
2018-09-21 19:08:50 +03:00
}
}
2021-12-17 22:59:54 +03:00
}