using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using Duplicati.Library.Logging;
namespace Duplicati.Server
{
///
/// Writes log messages to the Windows Event Log
///
[SupportedOSPlatform("windows")]
public class WindowsEventLogSource : ILogDestination, IDisposable
{
///
/// The event log to write to
///
private readonly EventLog m_eventLog;
///
/// Initializes a new instance of the class.
///
/// The source of the log messages
/// The log to write to
public WindowsEventLogSource(string source, string log = "Application")
{
m_eventLog = new EventLog
{
Source = source,
Log = log
};
}
///
/// Checks if the source exists
///
/// The source to check
/// True if the source exists
public static bool SourceExists(string source)
=> EventLog.SourceExists(source);
///
public void Dispose() => m_eventLog.Dispose();
///
public void WriteMessage(LogEntry entry)
=> m_eventLog.WriteEntry(entry.AsString(true), ToEventLogType(entry.Level));
///
/// Converts a log message type to an windows event log type
///
/// The log message type
/// The windows event log type
private static EventLogEntryType ToEventLogType(LogMessageType level)
{
return level switch
{
LogMessageType.ExplicitOnly => EventLogEntryType.Information,
LogMessageType.Profiling => EventLogEntryType.Information,
LogMessageType.Verbose => EventLogEntryType.Information,
LogMessageType.Retry => EventLogEntryType.Warning,
LogMessageType.Information => EventLogEntryType.Information,
LogMessageType.DryRun => EventLogEntryType.Information,
LogMessageType.Warning => EventLogEntryType.Warning,
LogMessageType.Error => EventLogEntryType.Error,
_ => EventLogEntryType.Information
};
}
}
}