public void LogEvent(int userId, EventType eventType, EventSeverity severity) { using (var db = new SAMEntities()) { try { var eventSeverity = severity.ToString(); var eventTypeDescription = eventType.ToString(); var eventTypeId = eventType.GetEnumValue(); var eventLog = new SAM1.EventLog { CreateDate = DateTime.Now, UserId = userId, EventSeverityId = (int)severity, MetaData = metaData, EventTypeId = eventTypeId, }; db.EventLogs.Add(eventLog); db.SaveChanges(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage + " " + x.PropertyName); var fullErrorMessage = string.Join(" => ", errorMessages); var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } }
protected virtual string CreateLogMessage(EventSeverity eventSeverity, string message, string codePoint, Dictionary <string, string> data, string callerMemberName, string callerFilePath, int callerLineNumber, DateTime now) { Dictionary <string, string> props = data.Safe(); if (!this.EmitAdditionalData) { props = new Dictionary <string, string>(); } this.AddCodepoint(message, codePoint, callerMemberName, callerFilePath, callerLineNumber, props); this.AddCallerFilePath(callerFilePath, props); this.AddCallerLineNumber(callerLineNumber, props); this.AddCallerMemberName(callerMemberName, props); if (this.EmitCorrelationContext) { props[nameof(this.CorrelationContext)] = this.CorrelationContext.Get(); } this.CreateLogMessageAppendSpecificTelemetry(props); string json = Newtonsoft.Json.JsonConvert.SerializeObject(props); string log = $"[{GetTimeString(now)}][{eventSeverity.ToString()}]{message}|{json}"; return(log); }
protected virtual void TraceDiagnostic( EventSeverity eventSeverity, string message, string codePoint, string serializedData) { Diag($"TRACE EMIT {eventSeverity.ToString()}/{message}/{codePoint ?? "ncp"}/{serializedData ?? "nsd"}"); }
public void AddEntry(string text, EventSeverity severity) { StringBuilder builder = new StringBuilder(); builder.Append(DateTime.Now).Append(" | "); builder.Append(severity.ToString()).Append(" | "); builder.Append(text); string record = builder.ToString(); lock (_sync) { eventQueue.Enqueue(record); } }
/// <summary> /// Logs an event /// </summary> /// <param name="es"> /// The event severity /// </param> /// <param name="eventDesc"> /// The event description /// </param> public void LogEvent(EventSeverity es, string eventDesc) { // Create the log line string logLine = String.Format( "{0} {1}: {2}\n", DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt"), es.ToString(), eventDesc); // Logging to a file if ((_logEndpoint & LogLocation.File) == LogLocation.File) { // Do we need to open a new log file? if (_fileHour != DateTime.Now.Hour) OpenLogFile(); // Log the event _fileWriter.Write(logLine); // Write the event to disk _fileWriter.Flush(); } // Logging to the console if ((_logEndpoint & LogLocation.Console) == LogLocation.Console) Console.Write(logLine); }
private void TraceToConsole(EventSeverity eventSeverity, string message, string codePoint, Dictionary <string, string> data, string callerMemberName, string callerFilePath, int callerLineNumber, DateTime eventTime, string correlationContext) { try { if (data == null || !this.EmitAdditionalData) { data = new Dictionary <string, string>(); } if (this.EmitCodePoint) { if (codePoint == null) { if (this.NullCodepointAction == EmptyCodepointAction.DoNothing) { // dont add to dictionary as it does not exist. } else { data[nameof(codePoint)] = this.NullCodepointActionDelegate(message, data, callerMemberName, callerFilePath, callerLineNumber); } } else { data[nameof(codePoint)] = codePoint; } } if (this.EmitCallerMemberName) { data[nameof(callerMemberName)] = callerMemberName; } if (this.EmitCallerFilePath) { data[nameof(callerFilePath)] = callerFilePath; } if (this.EmitCallerLineNumber) { data[nameof(callerLineNumber)] = callerLineNumber.ToString(); } if (this.EmitCorrelationContext) { data[nameof(CorrelationContext)] = correlationContext; } // write event ConsoleColor foregroundColor = this.GetForegroundColor(eventSeverity); ConsoleColor backgroundColor = this.GetBackgroundColor(eventSeverity); Console.ResetColor(); Console.Write('['); Write(GetTimeString(eventTime), foregroundColor, backgroundColor); Console.Write("]["); Write(eventSeverity.ToString(), foregroundColor, backgroundColor); Console.Write(']'); WriteLine(message, foregroundColor, backgroundColor); string json; if (data.Count > 0) { json = JsonConvert.SerializeObject(data, Formatting.Indented); Write("Additional Data: ", foregroundColor, backgroundColor); WriteLine(json, ConsoleColor.DarkGray, ConsoleColor.Black); Console.WriteLine(); } else { json = null; } this.TraceDiagnostic(eventSeverity, message, codePoint, json); } catch (Exception ex) { DiagnosticTrace.Instance.Error("An unexpected exception has occurred when attempting to send data to the console. See the Details for more information.", ex, "sOe8HdhzzkM"); } }