/// <summary> /// Writes a log entry. /// </summary> /// <typeparam name="TState"></typeparam> /// <param name="logLevel">Entry will be written on this level.</param> /// <param name="eventId">Id of the event.</param> /// <param name="state">The entry to be written. Can be also an object.</param> /// <param name="exception">The exception related to this entry.</param> /// <param name="formatter">Function to create a <c>string</c> message of the <paramref name="state" /> and <paramref name="exception" />.</param> public void Log <TState>( mslogging.LogLevel logLevel , mslogging.EventId eventId , TState state , Exception exception , Func <TState, Exception, string> formatter ) { if (!this.IsEnabled(logLevel)) { return; } if (state == null && exception == null) { return; } if (RollbarScope.Current != null && RollbarLocator.RollbarInstance.Config.MaxItems > 0 ) { RollbarScope.Current.IncrementLogItemsCount(); if (RollbarScope.Current.LogItemsCount == RollbarLocator.RollbarInstance.Config.MaxItems) { // the Rollbar SDK just reached MaxItems limit, report this fact and pause further logging within this scope: RollbarLocator.RollbarInstance.Warning(RollbarScope.MaxItemsReachedWarning); return; } else if (RollbarScope.Current.LogItemsCount > RollbarLocator.RollbarInstance.Config.MaxItems) { // the Rollbar SDK already exceeded MaxItems limit, do not log for this scope: return; } } // let's custom build the Data object that includes the exception // along with the current HTTP request context: string message = null; if (formatter != null) { message = formatter(state, exception); } Rollbar.DTOs.Body payloadBody = null; if (exception != null) { payloadBody = new DTOs.Body(exception); } else if (!string.IsNullOrWhiteSpace(message)) { payloadBody = new DTOs.Body(new DTOs.Message(message)); } else { return; //nothing to report... } Dictionary <string, object> customProperties = new Dictionary <string, object>(); customProperties.Add( "LogEventID" , $"{eventId.Id}" + (string.IsNullOrWhiteSpace(eventId.Name) ? string.Empty : $" ({eventId.Name})") ); if (exception != null && message != null) { customProperties.Add("LogMessage", message); } var currentContext = GetCurrentContext(); Dictionary <string, object> customRequestFields = null; if (currentContext != null) { customRequestFields = new Dictionary <string, object>(); customRequestFields.Add("httpRequestTimestamp", currentContext.Timestamp); if (currentContext.HttpAttributes != null) { customRequestFields.Add("httpRequestID", currentContext.HttpAttributes.RequestID); customRequestFields.Add("statusCode", currentContext.HttpAttributes.StatusCode); customRequestFields.Add("scheme", currentContext.HttpAttributes.Scheme); customRequestFields.Add("protocol", currentContext.HttpAttributes.Protocol); } } var requestDto = new DTOs.Request(customRequestFields, currentContext?.HttpAttributes); DTOs.Data dataDto = new DTOs.Data( config: RollbarLocator.RollbarInstance.Config , body: payloadBody , custom: customProperties , request: requestDto ) { Level = RollbarLogger.Convert(logLevel), }; // log the Data object (the exception + the HTTP request data): RollbarLocator.RollbarInstance.Log(dataDto); }
/// <summary> /// Writes a log entry. /// </summary> /// <typeparam name="TState"></typeparam> /// <param name="logLevel">Entry will be written on this level.</param> /// <param name="eventId">Id of the event.</param> /// <param name="state">The entry to be written. Can be also an object.</param> /// <param name="exception">The exception related to this entry.</param> /// <param name="formatter">Function to create a <c>string</c> message of the <paramref name="state" /> and <paramref name="exception" />.</param> public void Log <TState>( mslogging.LogLevel logLevel , mslogging.EventId eventId , TState state , Exception exception , Func <TState, Exception, string> formatter ) { if (!this.IsEnabled(logLevel)) { return; } if (state == null && exception == null) { return; } // let's custom build the Data object that includes the exception // along with the current HTTP request context: string message = null; if (formatter != null) { message = formatter(state, exception); } Rollbar.DTOs.Body payloadBody = null; if (!string.IsNullOrWhiteSpace(message)) { payloadBody = new DTOs.Body(new DTOs.Message(message)); } else { payloadBody = new DTOs.Body(exception); } Dictionary <string, object> customProperties = new Dictionary <string, object>(); customProperties.Add( "LogEventID" , $"{eventId.Id}" + (string.IsNullOrWhiteSpace(eventId.Name) ? string.Empty : $" ({eventId.Name})") ); if (exception != null && message != null) { customProperties.Add("LogMessage", message); } var currentContext = GetCurrentContext(); Dictionary <string, object> customRequestFields = null; if (currentContext != null) { customRequestFields = new Dictionary <string, object>(); customRequestFields.Add("httpRequestTimestamp", currentContext.Timestamp); if (currentContext.HttpAttributes != null) { customRequestFields.Add("httpRequestID", currentContext.HttpAttributes.RequestID); customRequestFields.Add("statusCode", currentContext.HttpAttributes.StatusCode); customRequestFields.Add("scheme", currentContext.HttpAttributes.Scheme); customRequestFields.Add("protocol", currentContext.HttpAttributes.Protocol); } } var requestDto = new DTOs.Request(customRequestFields, currentContext?.HttpAttributes); DTOs.Data dataDto = new DTOs.Data( config: RollbarLocator.RollbarInstance.Config , body: payloadBody , custom: customProperties , request: requestDto ) { Level = RollbarLogger.Convert(logLevel), }; // log the Data object (the exception + the HTTP request data): RollbarLocator.RollbarInstance.Log(dataDto); }