应用程序见解 - 不收集第一个跟踪

问题描述 投票:1回答:1

我有一个控制台应用程序将一些日志发送到App Insights:

public static async Task Main(string[] args)
{
    Logger.Log("test1");
    Logger.Log("test2");
    Logger.Log("test3");
    ...
}

Logger看起来像这样:

public static class Logger
{
    private static TelemetryClient _logger;

    public static void Log(string message)
    {
        _GetLogger().TrackTrace(message);
        _Flush();
    }

    private static TelemetryClient _GetLogger()
    {
        if (_logger is null) _logger = _GetTelemetryClient();
        return _logger;
    }

    ...
}

我发现第一条消息没有进入AI。其他人呢。

有什么想法吗?

c# azure logging azure-application-insights
1个回答
0
投票

您可以尝试下面的代码,它适用于我:

在Program.cs中:

        public static async Task Main(string[] args)
        {
            Logger.Log("test 1");
            Logger.Log("test 2");
            Logger.Log("test 3");

            await Logger.DoSomethingAsync();

        }

在Logger.cs中:

    public static class Logger
    {
        private static TelemetryClient _logger;

        public static void Log(string message)
        {
            _GetLogger().TrackTrace(message);
            _Flush();
        }

        public static TelemetryClient _GetLogger()
        {
            if (_logger is null)
            {
                TelemetryConfiguration.Active.InstrumentationKey = "your key";
                _logger = new TelemetryClient();

            }

            return _logger;

        }

        public static void _Flush()
        {
            if (_logger is null)
            {
                TelemetryConfiguration.Active.InstrumentationKey = "your key";
                _logger = new TelemetryClient();

            }

            _logger.Flush();
        }

        public static async Task DoSomethingAsync()
        {
            await Task.Delay(1000);
        }

    }

执行后,所有消息都显示在azure portal:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.