#define TRACE using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.Reflection; namespace ArchestrAServices.Common; public static class ServiceTrace { public static TraceSource DiagData { get; private set; } public static TraceSource DiagControl { get; private set; } public static TraceSource DiagCommand { get; private set; } public static TraceSource DiagException { get; private set; } public static TraceSource SecurityEvent { get; private set; } public static TraceSource DiagDiagnostics { get; private set; } public static TraceSource DiagCsv { get; private set; } static ServiceTrace() { DiagData = new TraceSource("DataFlowLogs"); DiagControl = new TraceSource("ControlFlowLogs"); DiagCommand = new TraceSource("CommandLogs"); DiagException = new TraceSource("ExceptionLogs"); SecurityEvent = new TraceSource("SecurityLogs"); DiagDiagnostics = new TraceSource("DiagnosticsLogs"); DiagCsv = new TraceSource("CSVDiagnostics"); } public static void SetLogIdentityAll(string identity) { DiagData.SetLogIdentity(identity); DiagControl.SetLogIdentity(identity); DiagCommand.SetLogIdentity(identity); DiagException.SetLogIdentity(identity); SecurityEvent.SetLogIdentity(identity); DiagDiagnostics.SetLogIdentity(identity); DiagCsv.SetLogIdentity(identity); } public static void SetLogIdentity(this TraceSource traceSource, [Localizable(false)] string identity) { if (traceSource == null || string.IsNullOrEmpty(identity)) { return; } foreach (TraceListener listener in traceSource.Listeners) { PropertyInfo property = listener.GetType().GetProperty("LoggingIdentity"); if (property != null && property.CanWrite && property.PropertyType == typeof(string)) { property.SetValue(listener, identity, null); } } } public static void LogInfo([Localizable(false)] string message) { DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, message); } public static void LogInfo([Localizable(false)] string format, params object[] args) { LogInfo(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogWarning([Localizable(false)] string message) { DiagException.TraceEvent(TraceEventType.Warning, 0, message); } public static void LogWarning([Localizable(false)] string format, params object[] args) { LogWarning(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogError([Localizable(false)] string message) { DiagException.TraceEvent(TraceEventType.Error, 0, message); } public static void LogError([Localizable(false)] string format, params object[] args) { LogError(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogCritical([Localizable(false)] string message) { DiagException.TraceEvent(TraceEventType.Critical, 0, message); } public static void LogCritical([Localizable(false)] string format, params object[] args) { LogCritical(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogVerbose([Localizable(false)] string message) { DiagDiagnostics.TraceEvent(TraceEventType.Verbose, 0, message); } public static void LogVerbose([Localizable(false)] string format, params object[] args) { LogVerbose(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogStart([Localizable(false)] string message) { SecurityEvent.TraceEvent(TraceEventType.Start, 0, message); } public static void LogStart([Localizable(false)] string format, params object[] args) { LogStart(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogStop([Localizable(false)] string message) { SecurityEvent.TraceEvent(TraceEventType.Stop, 0, message); } public static void LogStop([Localizable(false)] string format, params object[] args) { LogStop(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogSuspend([Localizable(false)] string message) { SecurityEvent.TraceEvent(TraceEventType.Suspend, 0, message); } public static void LogSuspend([Localizable(false)] string format, params object[] args) { LogSuspend(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogResume([Localizable(false)] string message) { SecurityEvent.TraceEvent(TraceEventType.Resume, 0, message); } public static void LogResume([Localizable(false)] string format, params object[] args) { LogResume(string.Format(CultureInfo.InvariantCulture, format, args)); } public static void LogCsv(params object[] args) { DiagCsv.TraceEvent(TraceEventType.Information, 0, string.Empty, args); } }