protected override void AsyncLogMessage(object row, Sheev.Common.BaseModels.IBaseContextModel context) { TrackingDetailLog logRequest = ((TrackingDetailLog)row); if (logRequest.TrackingHeaderLogRequest == null) { try { var restClient = new RestSharp.RestClient(context.ApiUrlSettings.Value.LT319_URL); RestSharp.RestRequest req = new RestSharp.RestRequest("/v2/Detail", RestSharp.Method.POST); var bodyJSON = JsonConvert.SerializeObject(logRequest.TrackingDetailLogRequest); req.AddHeader("Authorization", $"Bearer {logRequest.Token}"); req.AddParameter("application/json", bodyJSON, ParameterType.RequestBody); var resp = (RestSharp.RestResponse)restClient.Execute(req); if (resp.StatusCode != HttpStatusCode.NoContent) { Utilities.ErrorLogger.Report(JsonConvert.SerializeObject(resp), "ThreadedLogger_TrackingDetail.AsyncLogMessage()", context, logRequest.TrackingDetailLogRequest.TrackingGuid, System.Diagnostics.EventLogEntryType.Error); // throw new HttpResponseException() { StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, ReasonPhrase = resp.ErrorException.Message }; } } catch (Exception ex) { Utilities.ErrorLogger.Report(JsonConvert.SerializeObject(ex), "ThreadedLogger_TrackingDetail.AsyncLogMessage()", context, logRequest.TrackingDetailLogRequest.TrackingGuid, System.Diagnostics.EventLogEntryType.Error); } } else { try { var restClient = new RestSharp.RestClient(context.ApiUrlSettings.Value.LT319_URL); RestSharp.RestRequest req = new RestSharp.RestRequest("/v2/Header", RestSharp.Method.POST); var bodyJSON = JsonConvert.SerializeObject(logRequest.TrackingHeaderLogRequest); req.AddHeader("Authorization", $"Bearer {logRequest.Token}"); req.AddParameter("application/json", bodyJSON, ParameterType.RequestBody); var resp = (RestSharp.RestResponse)restClient.Execute(req); if (resp.StatusCode != HttpStatusCode.NoContent) { Utilities.ErrorLogger.Report(JsonConvert.SerializeObject(resp), "ThreadedLogger_TrackingHeader.AsyncLogMessage()", context, logRequest.TrackingHeaderLogRequest.TrackingGuid, System.Diagnostics.EventLogEntryType.Error); // throw new HttpResponseException() { StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, ReasonPhrase = resp.ErrorException.Message }; } } catch (Exception ex) { Utilities.ErrorLogger.Report(JsonConvert.SerializeObject(ex), "ThreadedLogger_TrackingHeader.AsyncLogMessage()", context, logRequest.TrackingHeaderLogRequest.TrackingGuid, System.Diagnostics.EventLogEntryType.Error); } } }
/// <summary> /// Log all events here /// </summary> /// <param name="details"></param> /// <param name="activity"></param> /// <param name="trackingStatus"></param> /// <param name="context"></param> /// <param name="trackingGuid"></param> /// <param name="logType"></param> public static void LogTrackingEvent(string details, string activity, LT319.Common.Utilities.Constants.TrackingStatus trackingStatus, Sheev.Common.BaseModels.IBaseContextModel context, Guid?trackingGuid) { try { string status = string.Empty; // Get the status for the activity based on the value passed in for trackingStatus switch (trackingStatus) { case LT319.Common.Utilities.Constants.TrackingStatus.Active: status = "Active"; break; case LT319.Common.Utilities.Constants.TrackingStatus.Complete: status = "Complete"; break; case LT319.Common.Utilities.Constants.TrackingStatus.Error: status = "Error"; break; } // Create a new Detail Request and build the request that will be added to the database LT319.Common.Models.DetailRequest logRequest = new LT319.Common.Models.DetailRequest() { Details = details, ST_ApplicationId = context.GenericSettings == null ? 0: (int)context.GenericSettings.Value.ApplicationId, Activity = activity, ActivityTimestamp = DateTimeOffset.Now, MappingCollectionTypeId = (int)Sheev.Common.Utilities.Constants.TM_MappingCollectionType.NONE, TrackingGuid = trackingGuid.Value, DetailStatus = status.ToString() }; // Create a new Tracking Detail Log Utilities.TrackingDetailLog trackingLogRequest = new TrackingDetailLog() { Token = context.Security.GetAuthToken(), TrackingDetailLogRequest = logRequest }; if (threadedTrackingLogger == null) { threadedTrackingLogger = new ThreadedLogger_TrackingDetail(); } //check if the application is logging the message don't have NLog set. if (context.NLogger != null) { //Log using NLog if (status == "Error") { context.NLogger.LogDetailMessage($"Log Detail ({status})", trackingLogRequest.TrackingDetailLogRequest, NLog.LogLevel.Error); } else { context.NLogger.LogDetailMessage($"Log Detail ({status})", trackingLogRequest.TrackingDetailLogRequest, NLog.LogLevel.Info); } } //If still needed to update the header for each detail created, then make sure //the header is null and call the below method(I think the only thing that //needs to be updated in the header is the 'Status' and 'EndDate. //Maybe better to create an endpoint 'UpdateHeader' // Log the message details //To remove logging to old logger comment the below statement threadedTrackingLogger.LogMessage(trackingLogRequest, context); } catch (Exception ex) { //string errorMessage = $"Error: {ex.Message} while trying to log: {details}"; string errorMessage = JsonConvert.SerializeObject(ex, Formatting.Indented); BackupLogger(errorMessage, "Logging.LogTrackingEvent()", EventLogEntryType.Error, context, trackingGuid); } }
/// <summary> /// Used to create a log tracking header and/or check/create tracking guid /// </summary> /// <param name="trackingGuid"></param> /// <param name="context"></param> /// <param name="requestTrackingGuid"></param> /// <param name="systemId"></param> /// <param name="internalId"></param> /// <returns></returns> public static Guid CreateLogTrackingHeader(Guid?trackingGuid, Sheev.Common.BaseModels.IBaseContextModel context, Guid?requestTrackingGuid = null, long?systemId = 0, long?internalId = 0) { // Create a new Guid var responseGuid = Guid.NewGuid(); // Check if trackingGuid passed in has a value and is not an empty guid, if it has a value, return that value if (trackingGuid.HasValue && trackingGuid != Guid.Empty) { return(trackingGuid.Value); } else if (requestTrackingGuid.HasValue && requestTrackingGuid != Guid.Empty) { return(requestTrackingGuid.Value); } // Create a new Header Request LT319.Common.Models.HeaderRequest logHeader = new LT319.Common.Models.HeaderRequest(); try { // Build the Header Request logHeader.TrackingGuid = responseGuid; logHeader.StartTimestamp = DateTimeOffset.Now; logHeader.Direction = (int)Sheev.Common.Utilities.Constants.TM_MappingDirection.TO_IPAAS; logHeader.SystemId = systemId; logHeader.MappingCollectionType = (int)Sheev.Common.Utilities.Constants.TM_MappingCollectionType.NONE; logHeader.Status = LT319.Common.Utilities.Constants.TrackingStatus.Active.ToString(); // Add Internal Id if a value for Internal Id was passed in if (internalId != null && internalId > 0) { logHeader.InternalId = internalId; } // Create a new Tracking Detail Log Utilities.TrackingDetailLog trackingLogRequest = new TrackingDetailLog() { Token = context.Security.GetAuthToken(), TrackingHeaderLogRequest = logHeader }; // Check if the threadedTrackingLogger has a value, if it does not, create a new one if (threadedTrackingLogger == null) { threadedTrackingLogger = new ThreadedLogger_TrackingDetail(); } //Log using NLog //check if the application is logging the message don't have NLog set. if (context.NLogger != null) { context.NLogger.LogHeaderMessage($"Log Header ({trackingLogRequest.TrackingHeaderLogRequest.TrackingGuid})", trackingLogRequest.TrackingHeaderLogRequest, NLog.LogLevel.Info); } // Log the tracking log request //To remove logging to old logger comment the below statement threadedTrackingLogger.LogMessage(trackingLogRequest, context); } catch (Exception ex) { string errorMessage = $"Error: {ex.Message} while trying to create activity header: GUID {trackingGuid}"; BackupLogger(errorMessage, "Logging.CreatLogTrackingHeader()", EventLogEntryType.Error, context, trackingGuid); } // Return the new Tracking Guid return(responseGuid); }