|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.ComponentModel.Composition; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Razor; |
| 10 | +using Microsoft.CodeAnalysis.Razor.Utilities; |
| 11 | +using Microsoft.VisualStudio.Shell; |
| 12 | +using Microsoft.VisualStudio.Shell.Interop; |
| 13 | +using Microsoft.VisualStudio.Threading; |
| 14 | + |
| 15 | +namespace Microsoft.VisualStudio.Razor.Logging; |
| 16 | + |
| 17 | +[Export(typeof(ActivityLog))] |
| 18 | +internal sealed class ActivityLog : IDisposable |
| 19 | +{ |
| 20 | + private enum EntryType { Error, Warning, Info } |
| 21 | + |
| 22 | + private readonly IAsyncServiceProvider _serviceProvider; |
| 23 | + private readonly JoinableTaskFactory _jtf; |
| 24 | + |
| 25 | + private readonly CancellationTokenSource _disposeTokenSource; |
| 26 | + private readonly AsyncBatchingWorkQueue<(EntryType, string)> _loggingQueue; |
| 27 | + private IVsActivityLog? _vsActivityLog; |
| 28 | + |
| 29 | + public ActivityLog( |
| 30 | + [Import(typeof(SAsyncServiceProvider))] IAsyncServiceProvider serviceProvider, |
| 31 | + JoinableTaskContext joinableTaskContext) |
| 32 | + { |
| 33 | + _serviceProvider = serviceProvider; |
| 34 | + _jtf = joinableTaskContext.Factory; |
| 35 | + |
| 36 | + _disposeTokenSource = new(); |
| 37 | + _loggingQueue = new AsyncBatchingWorkQueue<(EntryType, string)>(TimeSpan.Zero, ProcessBatchAsync, _disposeTokenSource.Token); |
| 38 | + } |
| 39 | + |
| 40 | + public void Dispose() |
| 41 | + { |
| 42 | + _disposeTokenSource.Cancel(); |
| 43 | + _disposeTokenSource.Dispose(); |
| 44 | + } |
| 45 | + |
| 46 | + private async ValueTask ProcessBatchAsync(ImmutableArray<(EntryType, string)> items, CancellationToken token) |
| 47 | + { |
| 48 | + await _jtf.SwitchToMainThreadAsync(token); |
| 49 | + |
| 50 | + _vsActivityLog ??= await _serviceProvider.GetServiceAsync<SVsActivityLog, IVsActivityLog>(); |
| 51 | + |
| 52 | + foreach (var (entryType, message) in items) |
| 53 | + { |
| 54 | + var vsEntryType = entryType switch |
| 55 | + { |
| 56 | + EntryType.Error => __ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, |
| 57 | + EntryType.Warning => __ACTIVITYLOG_ENTRYTYPE.ALE_WARNING, |
| 58 | + EntryType.Info => __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION, |
| 59 | + _ => Assumed.Unreachable<__ACTIVITYLOG_ENTRYTYPE>() |
| 60 | + }; |
| 61 | + |
| 62 | + _vsActivityLog.LogEntry( |
| 63 | + (uint)vsEntryType, |
| 64 | + "Razor", |
| 65 | + $"Info:{Environment.NewLine}{message}"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void LogError(string message) |
| 70 | + { |
| 71 | + _loggingQueue.AddWork((EntryType.Error, message)); |
| 72 | + } |
| 73 | + |
| 74 | + public void LogWarning(string message) |
| 75 | + { |
| 76 | + _loggingQueue.AddWork((EntryType.Warning, message)); |
| 77 | + } |
| 78 | + |
| 79 | + public void LogInfo(string message) |
| 80 | + { |
| 81 | + _loggingQueue.AddWork((EntryType.Info, message)); |
| 82 | + } |
| 83 | +} |
0 commit comments