{"id":21730100,"url":"https://github.com/nventive/httptracing","last_synced_at":"2025-10-07T12:31:17.473Z","repository":{"id":98348614,"uuid":"210605580","full_name":"nventive/HttpTracing","owner":"nventive","description":"Complete tracing of requests / responses for HttpClient","archived":false,"fork":false,"pushed_at":"2022-05-06T13:32:40.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-01-06T13:45:41.093Z","etag":null,"topics":["application-insights","csharp","httpclient","httpclientfactory","logging","netstandard","netstandard20"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nventive.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-24T13:07:59.000Z","updated_at":"2023-04-22T16:38:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"ba1db7c1-0fbd-49e6-b0da-4f69b3b19ac9","html_url":"https://github.com/nventive/HttpTracing","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FHttpTracing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FHttpTracing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FHttpTracing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nventive%2FHttpTracing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nventive","download_url":"https://codeload.github.com/nventive/HttpTracing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235628177,"owners_count":19020540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["application-insights","csharp","httpclient","httpclientfactory","logging","netstandard","netstandard20"],"created_at":"2024-11-26T04:11:54.550Z","updated_at":"2025-10-07T12:31:12.185Z","avatar_url":"https://github.com/nventive.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HttpTracing\n\nComplete tracing of requests / responses for `HttpClient`\n\n\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)\n[![Build Status](https://dev.azure.com/nventive-public/nventive/_apis/build/status/nventive.HttpTracing?branchName=master)](https://dev.azure.com/nventive-public/nventive/_build/latest?definitionId=9\u0026branchName=master)\n![Nuget](https://img.shields.io/nuget/v/HttpTracing)\n\n## Getting Started\n\nInstall the package:\n\n```\nInstall-Package HttpTracing\n```\n\n## Features\n\nWhen configuring the `HttpClient` via the factory, add the tracing handler:\n\n```csharp\n\nusing Microsoft.Extensions.DependencyInjection;\n\n\nservices\n    .AddHttpClient\u003cMyNamedClient\u003e() // Adds a named HttpClient\n    .AddHttpTracing(); // Attaches a tracing handler.\n```\n\nThe tracing handler (`AddHttpTracing`) should probably be the last handler in the\nchain in order to capture all modifications done by other handlers if they exist.\n\nThe logger category [follows the conventions defined by the `IHttpClientFactory`](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#logging)\nby naming the category `System.Net.Http.HttpClient.{HttpClient.Name}.TraceHandler`\n(e.g. `System.Net.Http.HttpClient.MyNamedClient.TraceHandler`).\n\nEvent ids used for the various messages:\n\n| Event id | Event Name         | Log level | Description                         |\n|----------|--------------------|-----------|-------------------------------------|\n| 200      | RequestSuccessful  | Trace     | Request trace on successful calls   |\n| 201      | RequestError       | Warning   | Request trace on unsuccessful calls |\n| 210      | ResponseSuccessful | Trace     | Response on successful calls        |\n| 211      | ResponseError      | Warning   | Response on unsuccessful calls      |\n\nA successful call is determined by default using `HttpResponseMessage.IsSuccessStatusCode`.\nThis can be customized when adding the handler:\n\n```csharp\nservices\n    .AddHttpClient\u003cMyNamedClient\u003e()\n    .AddHttpTracing(\n        isResponseSuccessful: response =\u003e response.StatusCode \u003e= HttpStatusCode.InternalServerError);\n```\n\nThe logger category name can also be customized if needed:\n\n```csharp\nservices\n    .AddHttpClient\u003cMyNamedClient\u003e()\n    .AddHttpTracing(categoryName: \"Foo.Bar\");\n```\n\n### Using with Application Insights\n\nBy default, Application Insights captures only `Warning` and `Error` log levels.\nTo enable tracing of successful requests and responses, [configure the log level for Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger). \nExample within the `appsettings.json` file:\n\n```json\n{\n  \"Logging\": {\n    \"ApplicationInsights\": {\n      \"LogLevel\": {\n        \"System.Net.Http.HttpClient.MyNamedClient.TraceHandler\": \"Trace\"\n      }\n    },\n  }\n}\n```\n\n### Buffering the requests content\n\nSome frameworks uses `HttpContent` streams on `HttpRequestMessage` that cannot be replayed, thus preventing the component\nto read the body of the request in such cases.\n\nThe default behavior is to write the following line in lieu of the body:\n```\n[InvalidOperationException: Request content is not buffered (...). Use the bufferRequests parameter to allow the reading.]\n```\n\nIf you want to see the request content in such cases, use the `bufferRequests` parameter:\n\n```csharp\nservices\n    .AddHttpClient\u003cMyNamedClient\u003e()\n    .AddHttpTracing(bufferRequests: true);\n```\n\n### Adding globally\n\nIt is possible to add tracing to **all** `HttpClient` registered through the `IHttpClientFactory`:\n\n```csharp\nservices.AddHttpTracingToAllHttpClients();\n```\n\nThis way, the tracing handler is added to all instances globally.\nTo customize the parameters, use the factory configuration method:\n\n```csharp\nservices.AddHttpTracingToAllHttpClients((sp, builder) =\u003e\n{\n    return builder.Name switch\n    {\n        nameof(BufferedClient) =\u003e new HttpMessageHandlerTracingConfiguration { BufferRequests = true },\n        nameof(DisabledClient) =\u003e new HttpMessageHandlerTracingConfiguration { Enabled = false },\n        _ =\u003e null, // Default configuration\n    };\n})\n```\n\n## Changelog\n\nPlease consult the [CHANGELOG](CHANGELOG.md) for more information about version\nhistory.\n\n## License\n\nThis project is licensed under the Apache 2.0 license - see the\n[LICENSE](LICENSE) file for details.\n\n## Contributing\n\nPlease read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for\ncontributing to this project.\n\nBe mindful of our [Code of Conduct](CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnventive%2Fhttptracing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnventive%2Fhttptracing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnventive%2Fhttptracing/lists"}