Detailed analysis of dotnet # monitor in. Net # 6 #

Time:2022-5-5

Original text:Announcing dotnet monitor in .NET 6

We first launched dotnet monitor as an experimental tool in June 2020 and worked hard to turn it into a production level tool last year (2020). Today, I am pleased to announce that the first official version of dotnet monitor will be released with Net 6 as Net 6.

Dotnet monitor has provided for azure app service (Linux) Net application, and we hope it can be used in more environments.

I What is dotnet monitor

Run in different environments Net applications can make it difficult to collect diagnostic information such as logs, traces, dumps. Dotnet monitor is a tool that provides a unified way to collect these diagnostic information, whether you are running on a desktop machine or a kubernetes cluster.

There are two different mechanisms for collecting this diagnostic information:

  • HTTP API to collect diagnostic information on demand. When your application encounters problems and wants to collect more information, you can call these HTTP APIs.
  • Triggers configured based on rules. You can configure rules to collect diagnostic information when the required conditions are met, such as process dump when the CPU maintains a high indicator for a period of time.

II introduction

Dotnet monitor can be obtained through two different distribution mechanisms:

  • . net cli tools
  • Container image obtained through Microsoft container Registry (MCR)

. net cli tools

The dotnet monitor cli tool needs to be installed first Net 6 SDK. If you don’t have enough new SDK, you can use. net download pageGet the installation package for installation.

You can use the following command to get the latest version of dotnet monitor:

dotnet tool install -g dotnet-monitor –version 6.0.0

If you have installed but want to update to the latest, you can run the following command:

dotnet tool update -g dotnet-monitor –version 6.0.0

Container mirroring

Dotnet monitor container image is available on MCR. You can obtain the latest image through the following command:

docker pull mcr.microsoft.com/dotnet/monitor:6.0.0

III HTTP API

Dotnet monitor exposes an HTTP API to query available processes, collect diagnostic information, and check the status of request information.

The following HTTP APIs are exposed:

  • /Processes – get the details of the processes that can be discovered
  • /Dump – captures the dump of a process without using a debugger
  • /Gcdump – GC dump of the capture process
  • /Trace – do not use the profiler to track the process
  • /Metrics – capture the indicator snapshot of the default process in Prometheus exposition format
  • /Livemetrics – capture the real-time indicator flow of the process
  • /Logs – capture the logs of the process
  • /Info – get information about dotnet monitor
  • /Operations – get operation status and cancel operation

The following example demonstrates how to use dotnet monitor to start from the target process and start from the target process in 60 secondsMicrosoft.AspNetCore.Server.Kestrel.ConnectionsLog stream data with the log level of debug.

PS> curl.exe -X POST “https://localhost:52323/logs?name=myWebApp&durationSeconds=60” `

    -H “Accept: application/x-ndjson” `

    -H “Content-Type: application/json” `

    –negotiate -u $(whoami)`

    -d ‘{“filterSpecs”: {“Microsoft.AspNetCore.Server.Kestrel.Connections”: “Debug”}}’ 

{“Timestamp”:”2021-11-05 08:12:54Z”,”LogLevel”:”Debug”,”EventId”:39,”EventName”:”ConnectionAccepted”,”Category”:”Microsoft.AspNetCore.Server.Kestrel.Connections”,”Message”:”Connection id u00220HMD06BUKL2CUu0022 accepted.”,”State”:{“Message”:”Connection id u00220HMD06BUKL2CUu0022 accepted.”,”ConnectionId”:”0HMD06BUKL2CU”,”{OriginalFormat}”:”Connection id u0022{ConnectionId}u0022 accepted.”}}

{“Timestamp”:”2021-11-05 08:12:54Z”,”LogLevel”:”Debug”,”EventId”:1,”EventName”:”ConnectionStart”,”Category”:”Microsoft.AspNetCore.Server.Kestrel.Connections”,”Message”:”Connection id u00220HMD06BUKL2CUu0022 started.”,”State”:{“Message”:”Connection id u00220HMD06BUKL2CUu0022 started.”,”ConnectionId”:”0HMD06BUKL2CU”,”{OriginalFormat}”:”Connection id u0022{ConnectionId}u0022 started.”}}

{“Timestamp”:”2021-11-05 08:12:54Z”,”LogLevel”:”Debug”,”EventId”:9,”EventName”:”ConnectionKeepAlive”,”Category”:”Microsoft.AspNetCore.Server.Kestrel.Connections”,”Message”:”Connection id u00220HMD06BUKL2CUu0022 completed keep alive response.”,”State”:{“Message”:”Connection id u00220HMD06BUKL2CUu0022 completed keep alive response.”,”ConnectionId”:”0HMD06BUKL2CU”,”{OriginalFormat}”:”Connection id u0022{ConnectionId}u0022 completed keep alive response.”},”Scopes”:[{“ConnectionId”:”0HMD06BUKL2CU”},{“RequestId”:”0HMD06BUKL2CU:00000002″,”RequestPath”:”/”}]}

As shown in the above example, you can use dotnet monitor to capture diagnostic information from the target process on demand. In addition to logs, you can also collect traces, memory dumps, GC dumps, and metrics from the target process.

IV trigger

Dotnet monitor can be configured to automatically collect diagnostic information based on conditions found in the process. When a new process is found, if the process data matches the rules, dotnet monitor will try to apply the configured rules. The applied rule will begin the process of monitoring the conditions described by the trigger. If this condition is met, the action list is executed assuming that the specified limit has not been reached.

Example: if dotnet monitor detects that the CPU utilization rate lasting for more than one minute is more than 80%, it will collect process dumps and limit them to no more than 1 per hour.


{
  "CollectionRules": {
    "HighCpuRule": {
      "Filters": [
        {
          "Key": "ProcessName",
          "Value": "MyApp",
          "MatchType": "Exact"
        }
      ],
      "Trigger": {
        "Type": "EventCounter",
        "Settings": {
          "ProviderName": "System.Runtime",
          "CounterName": "cpu-usage",
          "GreaterThan": 80,
          "SlidingWindowDuration": "00:01:00"
        }
      },
      "Limits": {
        "ActionCount": 1,
        "ActionCountSlidingWindowDuration": "1:00:00"
      },
      "Actions": [
        {
          "Type": "CollectDump",
          "Settings": {
            "Type": "Triage",
            "Egress": "myBlobStorageAccount"
          }
        }
      ]
    }
  }
}

Rule document:https://github.com/dotnet/dotnet-monitor/blob/main/documentation/collectionrules.md

V feedback

github issue: https://github.com/dotnet/dotnet-monitor/issues/new/choose

This is about Net 6 dotnet monitor article introduced here, more related Net6 dotnet monitor content, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!