Как проверить, загружаются ли пользовательские журналы в Azure Application Insights - PullRequest
0 голосов
/ 19 сентября 2018

Я пытаюсь добавить пользовательские события в Azure Application Insights, предоставив ключ инструментария следующим образом:

    public void WriteAgentLogs(string message, int AgentId, int ScheduleId)
    {
        TelemetryClient _telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration() { InstrumentationKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx" });
        _telemetryClient.InstrumentationKey = ""xxxxxxxxxxxxxxxxxxxxxxxxxx"";
        var parameters = new Dictionary<string, string>();

        parameters.Add("Message", message);
        parameters.Add("AgentId", AgentId.ToString());
        parameters.Add("ScheduleId", ScheduleId.ToString());
        parameters.Add("ScheduleStartDate", DateTime.Now.ToString());

        // check and add metrics here
        var metrics = new Dictionary<string, double>();
        if (duration != null)
        {
            metrics.Add("Duration", (double)duration);
        }

        _telemetryClient.TrackEvent("Agents", parameters, metrics);

        _telemetryClient.TrackTrace("Agents");

        _telemetryClient.TrackTrace(message, SeverityLevel.Information);
    }

После выполнения кода в окне вывода отображается следующее:

The program '[9672] LoadDataToAppInsights.exe' has exited with code 0 (0x0).
The program '[9672] LoadDataToAppInsights.exe: Program Trace' has exited with code 0 (0x0).

Когда я возвращаюсь на портал Azure и проверяю эти журналы, журналы не загружаются.Я не могу понять, где возникает проблема.Как я могу исправить эту проблему?

1 Ответ

0 голосов
/ 19 сентября 2018

Обновлено: сначала необходимо установить Microsoft.ApplicationInsights (здесь я устанавливаю последнюю версию 2.7.2) через nuget: enter image description here

Затем включите следующее пространство имен:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

Для консольного проекта вам необходимо добавить следующий код после метода Trackxx ():

    System.threading.thread.sleep(5000);
    _telemetryClient.Flush();

Вот мой пример кода:

        static void Main(string[] args)
        {
            TelemetryConfiguration.Active.InstrumentationKey = "your key";
            var telemetry = new TelemetryClient();

            var parameters = new Dictionary<string, string>();

            parameters.Add("Message", "message test");
            parameters.Add("AgentId", "Agent test");
            parameters.Add("ScheduleId", "schedule test");
            parameters.Add("ScheduleStartDate", DateTime.Now.ToString());

            var metrics = new Dictionary<string, double>();

                metrics.Add("Duration", 999.99);

            telemetry.TrackEvent("Agents event", parameters, metrics);

            telemetry.TrackTrace("Agents trace");

            telemetry.TrackTrace("message trace", SeverityLevel.Information);
            //telemetry.TrackTrace("0919 after today...");

            System.Threading.Thread.Sleep(5000);
            telemetry.Flush();

            Console.WriteLine("done now.");
            Console.ReadLine();
        }

После выполнения, если в окне вывода появляется следующее сообщение, означает успешную загрузку: enter image description here

Затем перейдите на портал Azure -> идеи вашего приложения -> Обзорный блейд -> нажмитепоиск, вы должны увидеть загруженные вами журналы (если показ на портале может занять несколько минут): enter image description here

Нажмите на журнал, чтобы увидеть подробности: enter image description here

Пожалуйста, дайте мне знать, если это работает.

...