private string GetExceptionJsonString(DateTime timestamp, AiException aiException) { var logRequest = new AiLogRequest { Name = $"Microsoft.ApplicationInsights.{this._instrumentationKey}.Exception", Time = timestamp.ToString("O"), InstrumentationKey = this._instrumentationKey, Tags = new AiTags { RoleInstance = null, OperationName = null, AuthenticatedUserId = this._authenticatedUserId }, Data = new AiData { BaseType = "ExceptionData", BaseData = new AiBaseData { Properties = this.EventProperties, Exceptions = new List <AiException> { aiException } } } }; var json = SerializationHelper.SerializeObject <AiLogRequest>(logRequest); return(json); }
/// <summary> /// Writes exception data to Application Insights. /// </summary> /// <param name="exception">The exception being logged.</param> /// <param name="aiExceptionSeverity">The severity level <see cref="AiExceptionSeverity"/>.</param> /// <param name="timestamp">The UTC timestamp of the event (default = DateTime.UtcNow).</param> /// <returns><c>true</c> if successfully logged, <c>false</c> otherwise.</returns> public bool WriteException(Exception exception, AiExceptionSeverity aiExceptionSeverity, DateTime?timestamp = null) { if (!Log("Exception", _disableExceptionTracking, _percentLoggedException)) { return(true); } timestamp = timestamp ?? DateTime.UtcNow; AiException aiException = new AiException(exception, aiExceptionSeverity); string json = GetExceptionJsonString(timestamp.Value, aiException); if (_enableDebug) { _tracingService.Trace($"DEBUG: Application Insights JSON: {CreateJsonDataLog(json)}"); } return(SendToAi(json)); }