aspnetcore.tsx 9.9 KB

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