aspnetcore.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import List from 'sentry/components/list';
  4. import ListItem from 'sentry/components/list/listItem';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import type {
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {
  12. getCrashReportModalConfigDescription,
  13. getCrashReportModalIntroduction,
  14. getCrashReportSDKInstallFirstStep,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  16. import {getDotnetMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  17. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  18. import {t, tct} from 'sentry/locale';
  19. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  20. type Params = DocsParams;
  21. const getInstallSnippetPackageManager = (params: Params) => `
  22. Install-Package Sentry.AspNetCore -Version ${getPackageVersion(
  23. params,
  24. 'sentry.dotnet.aspnetcore',
  25. params.isProfilingSelected ? '4.3.0' : '3.34.0'
  26. )}`;
  27. const getInstallSnippetCoreCli = (params: Params) => `
  28. dotnet add package Sentry.AspNetCore -v ${getPackageVersion(
  29. params,
  30. 'sentry.dotnet.aspnetcore',
  31. params.isProfilingSelected ? '4.3.0' : '3.34.0'
  32. )}`;
  33. const getInstallProfilingSnippetPackageManager = (params: Params) => `
  34. Install-Package Sentry.Profiling -Version ${getPackageVersion(
  35. params,
  36. 'sentry.dotnet.profiling',
  37. '4.3.0'
  38. )}`;
  39. const getInstallProfilingSnippetCoreCli = (params: Params) => `
  40. dotnet add package Sentry.Profiling -v ${getPackageVersion(
  41. params,
  42. 'sentry.dotnet.profiling',
  43. '4.3.0'
  44. )}`;
  45. const getConfigureSnippet = (params: Params) => `
  46. public static IHostBuilder CreateHostBuilder(string[] args) =>
  47. Host.CreateDefaultBuilder(args)
  48. .ConfigureWebHostDefaults(webBuilder =>
  49. {
  50. // Add the following line:
  51. webBuilder.UseSentry(o =>
  52. {
  53. o.Dsn = "${params.dsn}";
  54. // When configuring for the first time, to see what the SDK is doing:
  55. o.Debug = true;${
  56. params.isPerformanceSelected
  57. ? `
  58. // Set TracesSampleRate to 1.0 to capture 100%
  59. // of transactions for performance monitoring.
  60. // We recommend adjusting this value in production
  61. o.TracesSampleRate = 1.0;`
  62. : ''
  63. }${
  64. params.isProfilingSelected
  65. ? `
  66. // Sample rate for profiling, applied on top of othe TracesSampleRate,
  67. // e.g. 0.2 means we want to profile 20 % of the captured transactions.
  68. // We recommend adjusting this value in production.
  69. o.ProfilesSampleRate = 1.0;
  70. // Requires NuGet package: Sentry.Profiling
  71. // Note: By default, the profiler is initialized asynchronously. This can
  72. // be tuned by passing a desired initialization timeout to the constructor.
  73. o.AddIntegration(new ProfilingIntegration(
  74. // During startup, wait up to 500ms to profile the app startup code.
  75. // This could make launching the app a bit slower so comment it out if you
  76. // prefer profiling to start asynchronously.
  77. TimeSpan.FromMilliseconds(500)
  78. ));`
  79. : ''
  80. }
  81. });
  82. });`;
  83. const getPerformanceSpansSnippet = () => `
  84. using Microsoft.AspNetCore.Mvc;
  85. using Microsoft.Extensions.Logging;
  86. using Sentry;
  87. public class HomeController : Controller
  88. {
  89. private readonly IHub _sentryHub;
  90. public HomeController(IHub sentryHub) => _sentryHub = sentryHub;
  91. [HttpGet("/person/{id}")]
  92. public IActionResult Person(string id)
  93. {
  94. var childSpan = _sentryHub.GetSpan()?.StartChild("additional-work");
  95. try
  96. {
  97. // Do the work that gets measured.
  98. childSpan?.Finish(SpanStatus.Ok);
  99. }
  100. catch (Exception e)
  101. {
  102. childSpan?.Finish(e);
  103. throw;
  104. }
  105. }
  106. }`;
  107. const onboarding: OnboardingConfig = {
  108. install: params => [
  109. {
  110. type: StepType.INSTALL,
  111. description: tct('Install the [strong:NuGet] package:', {
  112. strong: <strong />,
  113. }),
  114. configurations: [
  115. {
  116. partialLoading: params.sourcePackageRegistries.isLoading,
  117. code: [
  118. {
  119. language: 'shell',
  120. label: 'Package Manager',
  121. value: 'packageManager',
  122. code: getInstallSnippetPackageManager(params),
  123. },
  124. {
  125. language: 'shell',
  126. label: '.NET Core CLI',
  127. value: 'coreCli',
  128. code: getInstallSnippetCoreCli(params),
  129. },
  130. ],
  131. },
  132. ...(params.isProfilingSelected
  133. ? [
  134. {
  135. code: [
  136. {
  137. language: 'shell',
  138. label: 'Package Manager',
  139. value: 'packageManager',
  140. code: getInstallProfilingSnippetPackageManager(params),
  141. },
  142. {
  143. language: 'shell',
  144. label: '.NET Core CLI',
  145. value: 'coreCli',
  146. code: getInstallProfilingSnippetCoreCli(params),
  147. },
  148. ],
  149. },
  150. ]
  151. : []),
  152. ],
  153. },
  154. ],
  155. configure: params => [
  156. {
  157. type: StepType.CONFIGURE,
  158. description: tct(
  159. 'Add Sentry to [programCode:Program.cs] through the [webHostCode:WebHostBuilder]:',
  160. {
  161. webHostCode: <code />,
  162. programCode: <code />,
  163. }
  164. ),
  165. configurations: [
  166. {
  167. language: 'csharp',
  168. code: getConfigureSnippet(params),
  169. },
  170. ],
  171. },
  172. ],
  173. verify: params => [
  174. {
  175. type: StepType.VERIFY,
  176. description: t('To verify your set up, you can capture a message with the SDK:'),
  177. configurations: [
  178. {
  179. language: 'csharp',
  180. code: 'SentrySdk.CaptureMessage("Hello Sentry");',
  181. },
  182. ],
  183. additionalInfo: tct(
  184. "If you don't want to depend on the static class, the SDK registers a client in the DI container. In this case, you can [link:take [code:IHub] as a dependency].",
  185. {
  186. code: <code />,
  187. link: (
  188. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/unit-testing/" />
  189. ),
  190. }
  191. ),
  192. },
  193. ...(params.isPerformanceSelected
  194. ? [
  195. {
  196. title: t('Performance Monitoring'),
  197. description: tct(
  198. 'You can measure the performance of your endpoints by adding a middleware to [code:Startup.cs]:',
  199. {
  200. code: <code />,
  201. }
  202. ),
  203. configurations: [
  204. {
  205. description: t(
  206. "You'll be able to monitor the performance of your actions automatically. To add additional spans to it, you can use the API:"
  207. ),
  208. language: 'csharp',
  209. code: getPerformanceSpansSnippet(),
  210. },
  211. ],
  212. },
  213. ]
  214. : []),
  215. {
  216. title: t('Samples'),
  217. description: (
  218. <Fragment>
  219. {t(
  220. 'See the following examples that demonstrate how to integrate Sentry with various frameworks.'
  221. )}
  222. <List symbol="bullet">
  223. <ListItem>
  224. {tct(
  225. '[link:Multiple samples in the [code:dotnet] SDK repository] [strong:(C#)]',
  226. {
  227. link: (
  228. <ExternalLink href="https://github.com/getsentry/sentry-dotnet/tree/main/samples" />
  229. ),
  230. code: <code />,
  231. strong: <strong />,
  232. }
  233. )}
  234. </ListItem>
  235. <ListItem>
  236. {tct('[link:Basic F# sample] [strong:(F#)]', {
  237. link: <ExternalLink href="https://github.com/sentry-demos/fsharp" />,
  238. strong: <strong />,
  239. })}
  240. </ListItem>
  241. <ListItem>
  242. {tct('[link:Giraffe F# sample] [strong:(F#)]', {
  243. link: <ExternalLink href="https://github.com/sentry-demos/giraffe" />,
  244. strong: <strong />,
  245. })}
  246. </ListItem>
  247. </List>
  248. </Fragment>
  249. ),
  250. },
  251. ],
  252. };
  253. const crashReportOnboarding: OnboardingConfig = {
  254. introduction: () => getCrashReportModalIntroduction(),
  255. install: (params: Params) => [
  256. {
  257. type: StepType.INSTALL,
  258. configurations: [
  259. getCrashReportSDKInstallFirstStep(params),
  260. {
  261. description: tct(
  262. 'If you are rendering the page from the server, for example on ASP.NET MVC, the [code:Error.cshtml] razor page can be:',
  263. {code: <code />}
  264. ),
  265. code: [
  266. {
  267. label: 'cshtml',
  268. value: 'html',
  269. language: 'html',
  270. code: `@using Sentry
  271. @using Sentry.AspNetCore
  272. @inject Microsoft.Extensions.Options.IOptions<SentryAspNetCoreOptions> SentryOptions
  273. @if (SentrySdk.LastEventId != SentryId.Empty) {
  274. <script>
  275. Sentry.init({ dsn: "@(SentryOptions.Value.Dsn)" });
  276. Sentry.showReportDialog({ eventId: "@SentrySdk.LastEventId" });
  277. </script>
  278. }`,
  279. },
  280. ],
  281. },
  282. ],
  283. },
  284. ],
  285. configure: () => [
  286. {
  287. type: StepType.CONFIGURE,
  288. description: getCrashReportModalConfigDescription({
  289. link: 'https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/user-feedback/configuration/#crash-report-modal',
  290. }),
  291. },
  292. ],
  293. verify: () => [],
  294. nextSteps: () => [],
  295. };
  296. const docs: Docs = {
  297. onboarding,
  298. replayOnboardingJsLoader,
  299. customMetricsOnboarding: getDotnetMetricsOnboarding({packageName: 'Sentry.AspNetCore'}),
  300. crashReportOnboarding,
  301. };
  302. export default docs;