Created
May 6, 2015 14:15
-
-
Save neuecc/3aff135009684e6af6fd to your computer and use it in GitHub Desktop.
Topshelf.EventSource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EventSourceConfiguratorExtensions | |
{ | |
public static void UseEventSource(this HostConfigurator configurator) | |
{ | |
HostLogger.UseLogger(new EventSourceHostLoggerConfigurator()); | |
} | |
} | |
[Serializable] | |
public class EventSourceHostLoggerConfigurator : HostLoggerConfigurator | |
{ | |
public LogWriterFactory CreateLogWriterFactory() | |
{ | |
return new EventSourceLogWriterFactory(); | |
} | |
} | |
public class EventSourceLogWriterFactory : LogWriterFactory | |
{ | |
public LogWriter Get(string name) | |
{ | |
EventSourceLogWriter.Instance.Get(name); | |
return EventSourceLogWriter.Instance; | |
} | |
public void Shutdown() | |
{ | |
EventSourceLogWriter.Instance.Shutdown(); | |
} | |
} | |
[EventSource(Name = "Topshelf")] | |
public class EventSourceLogWriter : EventSource, LogWriter | |
{ | |
public static readonly EventSourceLogWriter Instance = new EventSourceLogWriter(); | |
EventSourceLogWriter() | |
{ | |
} | |
public bool IsDebugEnabled | |
{ | |
get | |
{ | |
return this.IsEnabled(EventLevel.Verbose, EventKeywords.None); | |
} | |
} | |
public bool IsErrorEnabled | |
{ | |
get | |
{ | |
return this.IsEnabled(EventLevel.Error, EventKeywords.None); | |
} | |
} | |
public bool IsFatalEnabled | |
{ | |
get | |
{ | |
return this.IsEnabled(EventLevel.Critical, EventKeywords.None); | |
} | |
} | |
public bool IsInfoEnabled | |
{ | |
get | |
{ | |
return this.IsEnabled(EventLevel.Informational, EventKeywords.None); | |
} | |
} | |
public bool IsWarnEnabled | |
{ | |
get | |
{ | |
return this.IsEnabled(EventLevel.Warning, EventKeywords.None); | |
} | |
} | |
[Event(1, Level = EventLevel.Verbose)] | |
public void Debug(string message) | |
{ | |
WriteEvent(1, message ?? ""); | |
} | |
[Event(2, Level = EventLevel.Verbose)] | |
public void DebugException(string message, string exception) | |
{ | |
WriteEvent(2, message ?? "", exception ?? ""); | |
} | |
[Event(3, Level = EventLevel.Error)] | |
public void Error(string message) | |
{ | |
WriteEvent(3, message ?? ""); | |
} | |
[Event(4, Level = EventLevel.Error)] | |
public void ErrorException(string message, string exception) | |
{ | |
WriteEvent(4, message ?? "", exception ?? ""); | |
} | |
[Event(5, Level = EventLevel.Critical)] | |
public void Fatal(string message) | |
{ | |
WriteEvent(5, message ?? ""); | |
} | |
[Event(6, Level = EventLevel.Critical)] | |
public void FatalException(string message, string exception) | |
{ | |
WriteEvent(6, message ?? "", exception ?? ""); | |
} | |
[Event(7, Level = EventLevel.Informational)] | |
public void Info(string message) | |
{ | |
WriteEvent(7, message ?? ""); | |
} | |
[Event(8, Level = EventLevel.Informational)] | |
public void InfoException(string message, string exception) | |
{ | |
WriteEvent(8, message ?? "", exception ?? ""); | |
} | |
[Event(9, Level = EventLevel.Warning)] | |
public void Warn(string message) | |
{ | |
WriteEvent(9, message ?? ""); | |
} | |
[Event(10, Level = EventLevel.Warning)] | |
public void WarnException(string message, string exception) | |
{ | |
WriteEvent(10, message ?? "", exception ?? ""); | |
} | |
[Event(11, Level = EventLevel.Verbose)] | |
public void Get(string name) | |
{ | |
WriteEvent(11, name); | |
} | |
[Event(12, Level = EventLevel.Verbose)] | |
public void Shutdown() | |
{ | |
WriteEvent(12); | |
} | |
[NonEvent] | |
public void Debug(LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Debug((obj == null) ? "" : obj.ToString()); | |
} | |
[NonEvent] | |
public void Debug(object obj) | |
{ | |
Debug((obj == null) ? "" : ((obj as string) ?? obj.ToString())); | |
} | |
[NonEvent] | |
public void Debug(object obj, Exception exception) | |
{ | |
DebugException((obj == null) ? "" : ((obj as string) ?? obj.ToString()), exception.ToString()); | |
} | |
[NonEvent] | |
public void DebugFormat(string format, params object[] args) | |
{ | |
Debug(string.Format(format, args)); | |
} | |
[NonEvent] | |
public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Debug(string.Format(formatProvider, format, args)); | |
} | |
[NonEvent] | |
public void Error(LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Error((obj == null) ? "" : obj.ToString()); | |
} | |
[NonEvent] | |
public void Error(object obj) | |
{ | |
Error((obj == null) ? "" : ((obj as string) ?? obj.ToString())); | |
} | |
[NonEvent] | |
public void Error(object obj, Exception exception) | |
{ | |
ErrorException((obj == null) ? "" : ((obj as string) ?? obj.ToString()), exception.ToString()); | |
} | |
[NonEvent] | |
public void ErrorFormat(string format, params object[] args) | |
{ | |
Error(string.Format(format, args)); | |
} | |
[NonEvent] | |
public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Error(string.Format(formatProvider, format, args)); | |
} | |
[NonEvent] | |
public void Fatal(LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Fatal((obj == null) ? "" : obj.ToString()); | |
} | |
[NonEvent] | |
public void Fatal(object obj) | |
{ | |
Fatal((obj == null) ? "" : ((obj as string) ?? obj.ToString())); | |
} | |
[NonEvent] | |
public void Fatal(object obj, Exception exception) | |
{ | |
FatalException((obj == null) ? "" : ((obj as string) ?? obj.ToString()), exception.ToString()); | |
} | |
[NonEvent] | |
public void FatalFormat(string format, params object[] args) | |
{ | |
Fatal(string.Format(format, args)); | |
} | |
[NonEvent] | |
public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Fatal(string.Format(formatProvider, format, args)); | |
} | |
[NonEvent] | |
public void Info(LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Info((obj == null) ? "" : obj.ToString()); | |
} | |
[NonEvent] | |
public void Info(object obj) | |
{ | |
Info((obj == null) ? "" : ((obj as string) ?? obj.ToString())); | |
} | |
[NonEvent] | |
public void Info(object obj, Exception exception) | |
{ | |
InfoException((obj == null) ? "" : ((obj as string) ?? obj.ToString()), exception.ToString()); | |
} | |
[NonEvent] | |
public void InfoFormat(string format, params object[] args) | |
{ | |
Info(string.Format(format, args)); | |
} | |
[NonEvent] | |
public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Info(string.Format(formatProvider, format, args)); | |
} | |
[NonEvent] | |
public void Log(LoggingLevel level, LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Log(level, obj); | |
} | |
[NonEvent] | |
public void Log(LoggingLevel level, object obj) | |
{ | |
if (level == LoggingLevel.Fatal) Fatal(obj); | |
else if (level == LoggingLevel.Error) Error(obj); | |
else if (level == LoggingLevel.Warn) Warn(obj); | |
else if (level == LoggingLevel.Info) Info(obj); | |
else if (level == LoggingLevel.Debug) Debug(obj); | |
else if (level == LoggingLevel.All) | |
{ | |
if (IsDebugEnabled) Debug(obj); | |
else if (IsInfoEnabled) Info(obj); | |
else if (IsWarnEnabled) Warn(obj); | |
else if (IsErrorEnabled) Error(obj); | |
else if (IsFatalEnabled) Fatal(obj); | |
} | |
} | |
[NonEvent] | |
public void Log(LoggingLevel level, object obj, Exception exception) | |
{ | |
if (level == LoggingLevel.Fatal) Fatal(obj, exception); | |
else if (level == LoggingLevel.Error) Error(obj, exception); | |
else if (level == LoggingLevel.Warn) Warn(obj, exception); | |
else if (level == LoggingLevel.Info) Info(obj, exception); | |
else if (level == LoggingLevel.Debug) Debug(obj, exception); | |
else if (level == LoggingLevel.All) | |
{ | |
if (IsDebugEnabled) Debug(obj, exception); | |
else if (IsInfoEnabled) Info(obj, exception); | |
else if (IsWarnEnabled) Warn(obj, exception); | |
else if (IsErrorEnabled) Error(obj, exception); | |
else if (IsFatalEnabled) Fatal(obj, exception); | |
} | |
} | |
[NonEvent] | |
public void LogFormat(LoggingLevel level, string format, params object[] args) | |
{ | |
Log(level, string.Format(format, args)); | |
} | |
[NonEvent] | |
public void LogFormat(LoggingLevel level, IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Log(level, string.Format(formatProvider, format, args)); | |
} | |
[NonEvent] | |
public void Warn(LogWriterOutputProvider messageProvider) | |
{ | |
var obj = messageProvider(); | |
Warn((obj == null) ? "" : obj.ToString()); | |
} | |
[NonEvent] | |
public void Warn(object obj) | |
{ | |
Warn((obj == null) ? "" : ((obj as string) ?? obj.ToString())); | |
} | |
[NonEvent] | |
public void Warn(object obj, Exception exception) | |
{ | |
WarnException((obj == null) ? "" : ((obj as string) ?? obj.ToString()), exception.ToString()); | |
} | |
[NonEvent] | |
public void WarnFormat(string format, params object[] args) | |
{ | |
Warn(string.Format(format, args)); | |
} | |
[NonEvent] | |
public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) | |
{ | |
Warn(string.Format(formatProvider, format, args)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment