/// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <typeparam name="TState">State being passed along.</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>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (this.IsEnabled(logLevel))
            {
                if (exception == null || !this.applicationInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry)
                {
                    TraceTelemetry traceTelemetry = new TraceTelemetry(
                        formatter(state, exception),
                        ApplicationInsightsLogger.GetSeverityLevel(logLevel));
                    this.PopulateTelemetry(traceTelemetry, state, eventId);
                    this.telemetryClient.TrackTrace(traceTelemetry);
                }
                else
                {
                    ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception)
                    {
                        Message       = formatter(state, exception),
                        SeverityLevel = ApplicationInsightsLogger.GetSeverityLevel(logLevel),
                    };

                    this.PopulateTelemetry(exceptionTelemetry, state, eventId);
                    this.telemetryClient.TrackException(exceptionTelemetry);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <typeparam name="TState">State being passed along.</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>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            try
            {
                if (this.IsEnabled(logLevel))
                {
                    if (exception == null || !this.applicationInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry)
                    {
                        TraceTelemetry traceTelemetry = new TraceTelemetry(
                            formatter(state, exception),
                            ApplicationInsightsLogger.GetSeverityLevel(logLevel));
                        this.PopulateTelemetry(traceTelemetry, state, eventId);
                        if (exception != null)
                        {
                            traceTelemetry.Properties.Add("ExceptionMessage", exception.Message);
                            traceTelemetry.Properties.Add("ExceptionStackTrace", exception.ToInvariantString());
                        }

                        this.telemetryClient.TrackTrace(traceTelemetry);
                    }
                    else
                    {
                        ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(exception)
                        {
                            Message       = exception.Message,
                            SeverityLevel = ApplicationInsightsLogger.GetSeverityLevel(logLevel),
                        };

                        exceptionTelemetry.Properties.Add("FormattedMessage", formatter(state, exception));
                        this.PopulateTelemetry(exceptionTelemetry, state, eventId);
                        this.telemetryClient.TrackException(exceptionTelemetry);
                    }
                }
            }
            catch (Exception ex)
            {
                ApplicationInsightsLoggerEventSource.Log.FailedToLog(ex.ToInvariantString());
            }
        }