无法将 Sentry 添加到 .NET 6 中的 ASP.NET Core Web API 项目。

huangapple go评论74阅读模式
英文:

Can't add Sentry to ASP.NET Core Web API project in .NET 6

问题

我正在尝试将 Sentry 添加到我的 ASP.NET Core Web API 项目中,使用的是 .NET 6。然而,我遇到了一个问题,即无法在 IServiceCollection 中找到 AddSentry 方法,尽管我已安装了 Sentry.AspNetCore 包。

我已按照 Sentry 文档的说明操作,并将 SentryAspNetCore 包添加到了我的项目中。我还在我的 Program.cs 文件中添加了以下行:

builder.Services.AddSentry();

然而,在构建项目时,我收到了以下错误:

'IServiceCollection' 不包含 'AddSentry' 的定义,而最佳的扩展方法重载 'LoggingBuilderExtensions.AddSentry(ILoggingBuilder, string)' 要求接收类型为 'ILoggingBuilder'。

似乎 AddSentry 不被识别为 IServiceCollection 的扩展方法。我已经搜索了类似的问题,并找到一些建议添加 Sentry.Extensions.Logging 包,但那并没有帮助。

有人能帮我找出我做错了什么,以及如何将 Sentry 添加到我的 ASP.NET Core 6 Web API 项目中吗?提前感谢!

英文:

I am trying to add Sentry to my ASP.NET Core Web API project using .NET 6. However, I am encountering an issue where I cannot find the AddSentry method in IServiceCollection, even though I have installed the Sentry.AspNetCore package.

I have followed the instructions on the Sentry documentation and added the SentryAspNetCore package to my project. I also added the following line to my Program.cs file:

builder.Services.AddSentry();

However, I get the following error when building my project:

> 'IServiceCollection' does not contain a definition for 'AddSentry' and the best extension method overload 'LoggingBuilderExtensions.AddSentry(ILoggingBuilder, string)' requires a receiver of type 'ILoggingBuilder'

It seems that AddSentry is not recognized as an extension method of IServiceCollection. I have searched for similar issues and found some suggestions to add the Sentry.Extensions.Logging package, but that did not help.

Can anyone help me figure out what I am doing wrong and how I can add Sentry to my ASP.NET Core 6 Web API project? Thank you in advance!

答案1

得分: 3

要实现不仅仅是日志记录,还包括中间件和错误页面,请在 WebHostBuilder 上使用 UseSentry 扩展方法:

var builder = WebApplication.CreateBuilder(args);

// 在 Web 主机上调用 UseSentry。
builder.WebHost.UseSentry();

var app = builder.Build();
app.UseRouting();

// 您还可能需要以下内容,以捕获事务。
// 这必须位于 UseRouting 之后。
app.UseSentryTracing();

文档链接: https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/

英文:

To get not just logging but Middleware and error page, use the UseSentry extension method on the WebHostBuilder

var builder = WebApplication.CreateBuilder(args);

// Call UseSentry on the web host.
builder.WebHost.UseSentry();

var app = builder.Build();
app.UseRouting();


// You'll also likely want this, to capture transactions.
// This must be *after* UseRouting.
app.UseSentryTracing();

Docs: https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/

huangapple
  • 本文由 发表于 2023年2月19日 15:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定