maui.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Alert} from 'sentry/components/alert';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  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. getCrashReportGenericInstallStep,
  13. getCrashReportModalConfigDescription,
  14. getCrashReportModalIntroduction,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  16. import {getDotnetMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  17. import {csharpFeedbackOnboarding} from 'sentry/gettingStartedDocs/dotnet/dotnet';
  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.Maui -Version ${getPackageVersion(
  23. params,
  24. 'sentry.dotnet.maui',
  25. params.isProfilingSelected ? '4.3.0' : '3.34.0'
  26. )}`;
  27. const getInstallSnippetCoreCli = (params: Params) => `
  28. dotnet add package Sentry.Maui -v ${getPackageVersion(
  29. params,
  30. 'sentry.dotnet.maui',
  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. enum DotNetPlatform {
  46. WINDOWS = 0,
  47. IOS_MACCATALYST = 1,
  48. }
  49. const getConfigureSnippet = (params: Params, platform?: DotNetPlatform) => `
  50. public static MauiApp CreateMauiApp()
  51. {
  52. var builder = MauiApp.CreateBuilder();
  53. builder
  54. .UseMauiApp<App>()
  55. // Add this section anywhere on the builder:
  56. .UseSentry(options => {
  57. // The DSN is the only required setting.
  58. options.Dsn = "${params.dsn.public}";
  59. // Use debug mode if you want to see what the SDK is doing.
  60. // Debug messages are written to stdout with Console.Writeline,
  61. // and are viewable in your IDE's debug console or with 'adb logcat', etc.
  62. // This option is not recommended when deploying your application.
  63. options.Debug = true;${
  64. params.isPerformanceSelected
  65. ? `
  66. // Set TracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  67. // We recommend adjusting this value in production.
  68. options.TracesSampleRate = 1.0;`
  69. : ''
  70. }${
  71. params.isProfilingSelected
  72. ? `
  73. // Sample rate for profiling, applied on top of othe TracesSampleRate,
  74. // e.g. 0.2 means we want to profile 20 % of the captured transactions.
  75. // We recommend adjusting this value in production.
  76. options.ProfilesSampleRate = 1.0;${
  77. platform !== DotNetPlatform.IOS_MACCATALYST
  78. ? `
  79. // Requires NuGet package: Sentry.Profiling
  80. // Note: By default, the profiler is initialized asynchronously. This can
  81. // be tuned by passing a desired initialization timeout to the constructor.
  82. options.AddIntegration(new ProfilingIntegration(
  83. // During startup, wait up to 500ms to profile the app startup code.
  84. // This could make launching the app a bit slower so comment it out if you
  85. // prefer profiling to start asynchronously
  86. TimeSpan.FromMilliseconds(500)
  87. ));`
  88. : ''
  89. }`
  90. : ''
  91. }
  92. // Other Sentry options can be set here.
  93. })
  94. // ... the remainder of your MAUI app setup
  95. return builder.Build();
  96. }`;
  97. const getPerformanceMessageHandlerSnippet = () => `
  98. var httpHandler = new SentryHttpMessageHandler();
  99. var httpClient = new HttpClient(httpHandler);`;
  100. const getPerformanceInstrumentationSnippet = () => `
  101. // Transaction can be started by providing, at minimum, the name and the operation
  102. var transaction = SentrySdk.StartTransaction(
  103. "test-transaction-name",
  104. "test-transaction-operation"
  105. );
  106. // Transactions can have child spans (and those spans can have child spans as well)
  107. var span = transaction.StartChild("test-child-operation");
  108. // ...
  109. // (Perform the operation represented by the span/transaction)
  110. // ...
  111. span.Finish(); // Mark the span as finished
  112. transaction.Finish(); // Mark the transaction as finished and send it to Sentry`;
  113. const onboarding: OnboardingConfig = {
  114. install: params => [
  115. {
  116. type: StepType.INSTALL,
  117. description: tct('Install the [strong:NuGet] package:', {
  118. strong: <strong />,
  119. }),
  120. configurations: [
  121. {
  122. partialLoading: params.sourcePackageRegistries.isLoading,
  123. code: [
  124. {
  125. language: 'shell',
  126. label: 'Package Manager',
  127. value: 'packageManager',
  128. code: getInstallSnippetPackageManager(params),
  129. },
  130. {
  131. language: 'shell',
  132. label: '.NET Core CLI',
  133. value: 'coreCli',
  134. code: getInstallSnippetCoreCli(params),
  135. },
  136. ],
  137. },
  138. ...(params.isProfilingSelected
  139. ? [
  140. {
  141. description: tct(
  142. 'Additionally, for all platforms except iOS/Mac Catalyst, you need to add a dependency on the [sentryProfilingPackage:Sentry.Profiling] NuGet package.',
  143. {
  144. sentryProfilingPackage: <code />,
  145. }
  146. ),
  147. code: [
  148. {
  149. language: 'shell',
  150. label: 'Package Manager',
  151. value: 'packageManager',
  152. code: getInstallProfilingSnippetPackageManager(params),
  153. },
  154. {
  155. language: 'shell',
  156. label: '.NET Core CLI',
  157. value: 'coreCli',
  158. code: getInstallProfilingSnippetCoreCli(params),
  159. },
  160. ],
  161. },
  162. {
  163. description: (
  164. <AlertWithoutMarginBottom type="info">
  165. {t(
  166. 'Profiling for .NET Framework and .NET on Android are not supported.'
  167. )}
  168. </AlertWithoutMarginBottom>
  169. ),
  170. },
  171. ]
  172. : []),
  173. ],
  174. },
  175. ],
  176. configure: params => [
  177. {
  178. type: StepType.CONFIGURE,
  179. description: tct(
  180. 'Then add Sentry to [code:MauiProgram.cs] through the [code:MauiAppBuilder]:',
  181. {
  182. code: <code />,
  183. }
  184. ),
  185. configurations: [
  186. params.isProfilingSelected
  187. ? {
  188. code: [
  189. {
  190. language: 'csharp',
  191. label: 'Windows',
  192. value: 'Windows',
  193. code: getConfigureSnippet(params, DotNetPlatform.WINDOWS),
  194. },
  195. {
  196. language: 'csharp',
  197. label: 'iOS/Mac Catalyst',
  198. value: 'ios/macCatalyst',
  199. code: getConfigureSnippet(params, DotNetPlatform.IOS_MACCATALYST),
  200. },
  201. ],
  202. }
  203. : {
  204. language: 'csharp',
  205. code: getConfigureSnippet(params),
  206. },
  207. ],
  208. },
  209. ],
  210. verify: params => [
  211. {
  212. type: StepType.VERIFY,
  213. description: t(
  214. 'To verify your set up, you can capture a message with the SDK, anywhere in your code after the application is built, such as in a page constructor or button click event handler:'
  215. ),
  216. configurations: [
  217. {
  218. language: 'csharp',
  219. code: 'SentrySdk.CaptureMessage("Hello Sentry");',
  220. },
  221. ],
  222. },
  223. ...(params.isPerformanceSelected
  224. ? [
  225. {
  226. title: t('Tracing'),
  227. description: (
  228. <Fragment>
  229. {t(
  230. 'We do not yet have automatic performance instrumentation for .NET MAUI. We will be adding that in a future release. However, if desired you can still manually instrument parts of your application.'
  231. )}
  232. <p>
  233. {tct(
  234. 'For some parts of your code, [automaticInstrumentationLink:automatic instrumentation] is available across all of our .NET SDKs, and can be used with MAUI as well:',
  235. {
  236. automaticInstrumentationLink: (
  237. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/maui/tracing/instrumentation/automatic-instrumentation/" />
  238. ),
  239. }
  240. )}
  241. </p>
  242. </Fragment>
  243. ),
  244. configurations: [
  245. {
  246. description: tct(
  247. 'If your app uses [code:HttpClient], you can instrument your HTTP calls by passing our HTTP message handler:',
  248. {code: <code />}
  249. ),
  250. language: 'csharp',
  251. code: getPerformanceMessageHandlerSnippet(),
  252. },
  253. {
  254. description: (
  255. <Fragment>
  256. {t(
  257. 'If your app uses Entity Framework Core or SQL Client, we will automatically instrument that for you without any additional code.'
  258. )}
  259. <p>
  260. {tct(
  261. 'For other parts of your code, you can use [customInstrumentationLink:custom instrumentation], such as in the following example:',
  262. {
  263. customInstrumentationLink: (
  264. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/maui/tracing/instrumentation/custom-instrumentation/" />
  265. ),
  266. }
  267. )}
  268. </p>
  269. </Fragment>
  270. ),
  271. language: 'csharp',
  272. code: getPerformanceInstrumentationSnippet(),
  273. },
  274. ],
  275. },
  276. ]
  277. : []),
  278. {
  279. title: t('Sample Application'),
  280. description: tct(
  281. 'See the [mauiSampleLink:MAUI Sample in the [code:sentry-dotnet] repository].',
  282. {
  283. mauiSampleLink: (
  284. <ExternalLink href="https://github.com/getsentry/sentry-dotnet/tree/main/samples/Sentry.Samples.Maui" />
  285. ),
  286. code: <code />,
  287. }
  288. ),
  289. },
  290. ],
  291. };
  292. const crashReportOnboarding: OnboardingConfig = {
  293. introduction: () => getCrashReportModalIntroduction(),
  294. install: (params: Params) => getCrashReportGenericInstallStep(params),
  295. configure: () => [
  296. {
  297. type: StepType.CONFIGURE,
  298. description: getCrashReportModalConfigDescription({
  299. link: 'https://docs.sentry.io/platforms/dotnet/guides/maui/user-feedback/configuration/#crash-report-modal',
  300. }),
  301. },
  302. ],
  303. verify: () => [],
  304. nextSteps: () => [],
  305. };
  306. const docs: Docs = {
  307. onboarding,
  308. feedbackOnboardingCrashApi: csharpFeedbackOnboarding,
  309. customMetricsOnboarding: getDotnetMetricsOnboarding({packageName: 'Sentry.Maui'}),
  310. crashReportOnboarding,
  311. };
  312. export default docs;
  313. const AlertWithoutMarginBottom = styled(Alert)`
  314. margin-bottom: 0;
  315. `;