maui.tsx 11 KB

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