EtwLogger.cs 2.5 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

using System;
using System.Diagnostics.Tracing;
using System.Threading;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Internal.Log
{
    /// <summary>
    /// A logger that publishes events to ETW using an EventSource.
    /// </summary>
    internal sealed class EtwLogger : ILogger
    {
15
        private readonly Lazy<Func<FunctionId, bool>> _loggingChecker;
16 17 18

        // Due to ETW specifics, RoslynEventSource.Instance needs to be initialized during EtwLogger construction 
        // so that we can enable the listeners synchronously before any events are logged.
19
        private readonly RoslynEventSource _source = RoslynEventSource.Instance;
P
Pilchie 已提交
20

21
        public EtwLogger(IGlobalOptionService optionService)
P
Pilchie 已提交
22
        {
23
            _loggingChecker = new Lazy<Func<FunctionId, bool>>(() => Logger.GetLoggingChecker(optionService));
P
Pilchie 已提交
24 25
        }

H
heejaechang 已提交
26
        public EtwLogger(Func<FunctionId, bool> loggingChecker)
P
Pilchie 已提交
27
        {
28
            _loggingChecker = new Lazy<Func<FunctionId, bool>>(() => loggingChecker);
P
Pilchie 已提交
29 30
        }

H
heejaechang 已提交
31
        public bool IsEnabled(FunctionId functionId)
P
Pilchie 已提交
32
        {
33
            return _source.IsEnabled() && _loggingChecker.Value(functionId);
P
Pilchie 已提交
34 35
        }

36
        public void Log(FunctionId functionId, LogMessage logMessage)
P
Pilchie 已提交
37
        {
38
            _source.Log(GetMessage(logMessage), functionId);
P
Pilchie 已提交
39 40
        }

41
        public void LogBlockStart(FunctionId functionId, LogMessage logMessage, int uniquePairId, CancellationToken cancellationToken)
P
Pilchie 已提交
42
        {
43
            RoslynEventSource.Instance.BlockStart(GetMessage(logMessage), functionId, uniquePairId);
P
Pilchie 已提交
44 45
        }

H
heejaechang 已提交
46
        public void LogBlockEnd(FunctionId functionId, LogMessage logMessage, int uniquePairId, int delta, CancellationToken cancellationToken)
P
Pilchie 已提交
47
        {
48
            if (cancellationToken.IsCancellationRequested)
P
Pilchie 已提交
49
            {
50
                RoslynEventSource.Instance.BlockCanceled(functionId, delta, uniquePairId);
P
Pilchie 已提交
51
            }
52
            else
P
Pilchie 已提交
53
            {
54
                RoslynEventSource.Instance.BlockStop(functionId, delta, uniquePairId);
P
Pilchie 已提交
55
            }
56
        }
P
Pilchie 已提交
57

58 59 60
        private bool IsVerbose()
        {
            // "-1" makes this to work with any keyword
61
            return _source.IsEnabled(EventLevel.Verbose, (EventKeywords)(-1));
62
        }
P
Pilchie 已提交
63

64 65 66
        private string GetMessage(LogMessage logMessage)
        {
            return IsVerbose() ? logMessage.GetMessage() : string.Empty;
P
Pilchie 已提交
67 68 69
        }
    }
}