|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using NuGet.Services.Logging; |
| 11 | +using NuGetGallery.Diagnostics; |
| 12 | + |
| 13 | +namespace NuGet.Jobs.Validation |
| 14 | +{ |
| 15 | + public class LoggerDiagnosticsSource : IDiagnosticsSource |
| 16 | + { |
| 17 | + private readonly ITelemetryClient _telemetryClient; |
| 18 | + private readonly ILogger _logger; |
| 19 | + |
| 20 | + public LoggerDiagnosticsSource(ITelemetryClient telemetryClient, ILogger logger) |
| 21 | + { |
| 22 | + _telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient)); |
| 23 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 24 | + } |
| 25 | + |
| 26 | + public void ExceptionEvent(Exception exception) |
| 27 | + { |
| 28 | + _telemetryClient.TrackException(exception); |
| 29 | + } |
| 30 | + |
| 31 | + public void PerfEvent( |
| 32 | + string name, |
| 33 | + TimeSpan time, |
| 34 | + IEnumerable<KeyValuePair<string, object>> payload) |
| 35 | + { |
| 36 | + _telemetryClient.TrackMetric( |
| 37 | + name, |
| 38 | + time.TotalMilliseconds, |
| 39 | + GetProperties(payload)); |
| 40 | + } |
| 41 | + |
| 42 | + public void TraceEvent( |
| 43 | + TraceEventType type, |
| 44 | + int id, |
| 45 | + string message, |
| 46 | + [CallerMemberName] string member = null, |
| 47 | + [CallerFilePath] string file = null, |
| 48 | + [CallerLineNumber] int line = 0) |
| 49 | + { |
| 50 | + _logger.Log( |
| 51 | + TraceEventTypeToLogLevel(type), |
| 52 | + id, |
| 53 | + state: new TraceState(type, id, message, member, file, line), |
| 54 | + exception: null, |
| 55 | + formatter: (s, e) => s.Message); |
| 56 | + } |
| 57 | + |
| 58 | + private static LogLevel TraceEventTypeToLogLevel(TraceEventType type) |
| 59 | + { |
| 60 | + switch (type) |
| 61 | + { |
| 62 | + case TraceEventType.Critical: |
| 63 | + return LogLevel.Critical; |
| 64 | + case TraceEventType.Error: |
| 65 | + return LogLevel.Error; |
| 66 | + case TraceEventType.Warning: |
| 67 | + return LogLevel.Warning; |
| 68 | + case TraceEventType.Information: |
| 69 | + return LogLevel.Information; |
| 70 | + default: |
| 71 | + return LogLevel.Trace; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static IDictionary<string, string> GetProperties( |
| 76 | + IEnumerable<KeyValuePair<string, object>> payload) |
| 77 | + { |
| 78 | + if (payload == null) |
| 79 | + { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + var dictionary = new Dictionary<string, string>(); |
| 84 | + foreach (var pair in payload) |
| 85 | + { |
| 86 | + dictionary[pair.Key] = pair.Value?.ToString(); |
| 87 | + } |
| 88 | + |
| 89 | + return dictionary; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// The parameters provided to <see cref="TraceEvent(TraceEventType, int, string, string, string, int)"/>. This |
| 94 | + /// type implements <see cref="IEnumerable{KeyValuePair{string, object}}" /> because Serilog uses this to |
| 95 | + /// populate the Application Insights custom dimensions property bag. |
| 96 | + /// </summary> |
| 97 | + public class TraceState : IEnumerable<KeyValuePair<string, object>> |
| 98 | + { |
| 99 | + private readonly Dictionary<string, object> _pairs; |
| 100 | + |
| 101 | + public TraceState(TraceEventType traceEventType, int id, string message, string member, string file, int line) |
| 102 | + { |
| 103 | + TraceEventType = traceEventType; |
| 104 | + Id = id; |
| 105 | + Message = message; |
| 106 | + Member = member; |
| 107 | + File = file; |
| 108 | + Line = line; |
| 109 | + |
| 110 | + _pairs = new Dictionary<string, object> |
| 111 | + { |
| 112 | + { nameof(TraceEventType), TraceEventType }, |
| 113 | + { nameof(Id), Id }, |
| 114 | + { nameof(Message), Message }, |
| 115 | + { nameof(Member), Member }, |
| 116 | + { nameof(File), File }, |
| 117 | + { nameof(Line), Line }, |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + public TraceEventType TraceEventType { get; } |
| 122 | + public int Id { get; } |
| 123 | + public string Message { get; } |
| 124 | + public string Member { get; } |
| 125 | + public string File { get; } |
| 126 | + public int Line { get; } |
| 127 | + |
| 128 | + public IEnumerator<KeyValuePair<string, object>> GetEnumerator() |
| 129 | + { |
| 130 | + return _pairs.GetEnumerator(); |
| 131 | + } |
| 132 | + |
| 133 | + IEnumerator IEnumerable.GetEnumerator() |
| 134 | + { |
| 135 | + return _pairs.GetEnumerator(); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments