/// <summary> /// Write entry to Windows EventLog /// </summary> /// <param name="entry">Entry to write</param> public void WriteEntry(IEntry entry) { if (entry.Category != "JKLog" && (entry.Context as Type) != typeof(WindowsEvent)) { try { // if there is a match between entry type and windows type EventLogEntryType type = EventLogEntryType.Information; if (Enum.IsDefined(typeof(EventLogEntryType), (int)entry.Type)) { type = (EventLogEntryType)entry.Type; } // context to byte array byte[] toBytes = new byte[] { }; if (entry.Context != null) { toBytes = Encoding.ASCII.GetBytes(entry.Context.ToString()); } // write to eventlog EventLog.WriteEntry(this.Source, entry.Message, type, (int)entry.Type, (short)entry.Type, toBytes); } catch (Exception) { JKLogger.FailureAudit("Failed to open Windows Event source. Source is not registered, configured or there is an internal failure in WindowsEvent mapper.", typeof(WindowsEvent), "JKLog"); } } }
/// <summary> /// Register Windows EventLog source. /// <para>You can call this at anytime, does nothing if source is already registered.</para> /// </summary> public void RegisterSource() { try { // If source does not exists. if (!EventLog.SourceExists(this.Source)) { EventLog.CreateEventSource(this.Source, this.Source); JKLogger.SuccessAudit("Windows Event source \"" + this.Source + "\" is created. Restart the application to allow it to be registered.", typeof(WindowsEvent), "JKLog"); return; } } catch (Exception) { JKLogger.FailureAudit("Failed to create Windows Event source. Call RegisterSource() while running as Administrator.", typeof(WindowsEvent), "JKLog"); } }
public static void UseTests() { // simple JKLogger.Error("This is Error"); JKLogger.Warning("This is Warning"); JKLogger.Information("This is Information"); JKLogger.SuccessAudit("This is SuccessAudit"); JKLogger.FailureAudit("This is FailureAudit"); JKLogger.Debug("This is Debug"); User testContext = new User("StaticTestName", 99); // more advanced JKLogger.Error("This is Error", testContext, "TestCategory"); JKLogger.Warning("This is Warning", testContext, "TestCategory"); JKLogger.Information("This is Information", testContext, "TestCategory"); JKLogger.SuccessAudit("This is SuccessAudit", testContext, "TestCategory"); JKLogger.FailureAudit("This is FailureAudit", testContext, "TestCategory"); JKLogger.Debug("This is Debug", testContext, "TestCategory"); }