maui.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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}";
  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 performance monitoring.
  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 [mauiProgram:MauiProgram.cs] through the [mauiAppBuilderCode:MauiAppBuilder]:',
  181. {
  182. mauiAppBuilderCode: <code />,
  183. mauiProgram: <code />,
  184. }
  185. ),
  186. configurations: [
  187. params.isProfilingSelected
  188. ? {
  189. code: [
  190. {
  191. language: 'csharp',
  192. label: 'Windows',
  193. value: 'Windows',
  194. code: getConfigureSnippet(params, DotNetPlatform.WINDOWS),
  195. },
  196. {
  197. language: 'csharp',
  198. label: 'iOS/Mac Catalyst',
  199. value: 'ios/macCatalyst',
  200. code: getConfigureSnippet(params, DotNetPlatform.IOS_MACCATALYST),
  201. },
  202. ],
  203. }
  204. : {
  205. language: 'csharp',
  206. code: getConfigureSnippet(params),
  207. },
  208. ],
  209. },
  210. ],
  211. verify: params => [
  212. {
  213. type: StepType.VERIFY,
  214. description: t(
  215. '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:'
  216. ),
  217. configurations: [
  218. {
  219. language: 'csharp',
  220. code: 'SentrySdk.CaptureMessage("Hello Sentry");',
  221. },
  222. ],
  223. },
  224. ...(params.isPerformanceSelected
  225. ? [
  226. {
  227. title: t('Performance Monitoring'),
  228. description: (
  229. <Fragment>
  230. {t(
  231. '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.'
  232. )}
  233. <p>
  234. {tct(
  235. '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:',
  236. {
  237. automaticInstrumentationLink: (
  238. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/maui/performance/instrumentation/automatic-instrumentation/" />
  239. ),
  240. }
  241. )}
  242. </p>
  243. </Fragment>
  244. ),
  245. configurations: [
  246. {
  247. description: tct(
  248. 'If your app uses [code:HttpClient], you can instrument your HTTP calls by passing our HTTP message handler:',
  249. {code: <code />}
  250. ),
  251. language: 'csharp',
  252. code: getPerformanceMessageHandlerSnippet(),
  253. },
  254. {
  255. description: (
  256. <Fragment>
  257. {t(
  258. 'If your app uses Entity Framework Core or SQL Client, we will automatically instrument that for you without any additional code.'
  259. )}
  260. <p>
  261. {tct(
  262. 'For other parts of your code, you can use [customInstrumentationLink:custom instrumentation], such as in the following example:',
  263. {
  264. customInstrumentationLink: (
  265. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/maui/performance/instrumentation/custom-instrumentation/" />
  266. ),
  267. }
  268. )}
  269. </p>
  270. </Fragment>
  271. ),
  272. language: 'csharp',
  273. code: getPerformanceInstrumentationSnippet(),
  274. },
  275. ],
  276. },
  277. ]
  278. : []),
  279. {
  280. title: t('Sample Application'),
  281. description: tct(
  282. 'See the [mauiSampleLink:MAUI Sample in the [code:sentry-dotnet] repository].',
  283. {
  284. mauiSampleLink: (
  285. <ExternalLink href="https://github.com/getsentry/sentry-dotnet/tree/main/samples/Sentry.Samples.Maui" />
  286. ),
  287. code: <code />,
  288. }
  289. ),
  290. },
  291. ],
  292. };
  293. const crashReportOnboarding: OnboardingConfig = {
  294. introduction: () => getCrashReportModalIntroduction(),
  295. install: (params: Params) => getCrashReportGenericInstallStep(params),
  296. configure: () => [
  297. {
  298. type: StepType.CONFIGURE,
  299. description: getCrashReportModalConfigDescription({
  300. link: 'https://docs.sentry.io/platforms/dotnet/guides/maui/user-feedback/configuration/#crash-report-modal',
  301. }),
  302. },
  303. ],
  304. verify: () => [],
  305. nextSteps: () => [],
  306. };
  307. const docs: Docs = {
  308. onboarding,
  309. feedbackOnboardingCrashApi: csharpFeedbackOnboarding,
  310. customMetricsOnboarding: getDotnetMetricsOnboarding({packageName: 'Sentry.Maui'}),
  311. crashReportOnboarding,
  312. };
  313. export default docs;
  314. const AlertWithoutMarginBottom = styled(Alert)`
  315. margin-bottom: 0;
  316. `;