Ejemplo n.º 1
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));
            }

            if (this.IsEnabled(logLevel))
            {
                if (exception == null)
                {
                    if (this.applicationInsightsLoggerOptions.UseCustomEvents)
                    {
                        var eventRecord = new EventTelemetryRecord(
                            formatter(state, null));

                        this.PopulateTelemetry(eventRecord, state, eventId);
                        this.telemetryClient.TrackEvent(eventRecord);

                        return;
                    }

                    var record = new TraceTelemetryRecord(
                        formatter(state, exception),
                        logLevel);

                    this.PopulateTelemetry(record, state, eventId);
                    if (exception != null)
                    {
                        record.Properties.Add("ExceptionMessage", exception.Message);
                    }

                    this.telemetryClient.TrackTrace(record);
                    return;
                }

                var exceptionTelemetry = new ExceptionTelemetryRecord(exception)
                {
                    Message       = exception.Message,
                    SeverityLevel = logLevel,
                };

                exceptionTelemetry.Properties.Add("FormattedMessage", formatter(state, exception));
                this.PopulateTelemetry(exceptionTelemetry, state, eventId);
                this.telemetryClient.TrackException(exceptionTelemetry);
            }
        }
        /// <summary>
        /// Send a trace.
        /// </summary>
        /// <param name="traceTelemetry">The trace record.</param>
        public void TrackTrace(TraceTelemetryRecord traceTelemetry)
        {
            if (traceTelemetry is null)
            {
                throw new ArgumentNullException(nameof(traceTelemetry));
            }

            var t = new TraceTelemetry(
                traceTelemetry.Message,
                this.GetSeverityLevel(traceTelemetry.SeverityLevel));

            foreach (var k in traceTelemetry.Properties.Keys)
            {
                t.Properties.Add(k, traceTelemetry.Properties[k]);
            }

            t.Timestamp = traceTelemetry.Timestamp;
            t.Sequence  = traceTelemetry.Sequence;

            this.client.TrackTrace(t);
        }