Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test/**/*.apk
/tools/
*.log
.sentry-native
**/EnvironmentVariables.g.cs

# Download cache for Cocoa SDK
modules/sentry-cocoa
5 changes: 5 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<DefineConstants>$(DefineConstants);CI_BUILD</DefineConstants>
</PropertyGroup>

<!-- So we know at build time whether the DSN has been provided in an environment variable or not -->
<PropertyGroup Condition=" '$(SENTRY_DSN)' != '' ">
<DefineConstants>$(DefineConstants);SENTRY_DSN_DEFINED_IN_ENV</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="UnoptimizedAssemblyDetector" Version="0.1.1" PrivateAssets="All" />
<PackageReference Include="Roslynator.Analyzers" Version="4.9.0" PrivateAssets="All" />
Expand Down
40 changes: 40 additions & 0 deletions samples/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,44 @@
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry.Bindings.Cocoa\buildTransitive\Sentry.Bindings.Cocoa.targets"
Condition="'$(OutputType)' == 'Exe' And ('$(TargetPlatformIdentifier)' == 'ios' Or '$(TargetPlatformIdentifier)' == 'maccatalyst')" />
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry\Platforms\Native\buildTransitive\Sentry.Native.targets" />

<Target Name="GenerateSharedDsnConstant"
BeforeTargets="BeforeCompile"
Condition=" '$(SENTRY_DSN)' != '' and '$(PlatformIsMobile)' == 'true'">

<Message Text="Generating shared EnvironmentVariables.g.cs with embedded DSN..." Importance="High" />

<WriteLinesToFile
File="$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs"
Lines="
// This file is auto-generated by a custom build target used in the Sentry Samples... in your own projects you should
// specify the DSN either directly in code (in the file where you initialize Sentry) or via SENTRY_DSN environment
// variable (for non-mobile projects - that won't work on Android, iOS, or MacCatalyst).
namespace Sentry.Samples%3B

internal static class EnvironmentVariables
{
/// &lt;summary&gt;
/// To make things easier for the SDK maintainers we have a custom build target that writes the
/// SENTRY_DSN environment variable into an EnvironmentVariables class that is available for mobile
/// targets. This allows us to share one DSN defined in the ENV across desktop and mobile samples.
/// Generally, you won't want to do this in your own mobile projects though - you should set the DSN
/// in code as above
/// &lt;/summary&gt;
internal const string Dsn = &quot;$(SENTRY_DSN)&quot;%3B
}
"
Overwrite="true" />
</Target>

<ItemGroup>
<!-- Make sure all sample projects include the generated file -->
<Compile Include="$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs" Condition="Exists('$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs')" />
<!-- Make sure all sample projects include any other shared files -->
<Compile Include="$(MSBuildThisFileDirectory)SamplesShared.cs" />

<!-- Add shared global usings -->
<Using Include="Sentry.Samples" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions samples/SamplesShared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Sentry.Samples;

public static class SamplesShared
{
#if !SENTRY_DSN_DEFINED_IN_ENV
/// <summary>
/// <para>
/// You must specify a DSN. See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
/// </para>
/// <para>
/// On mobile platforms (iOS, Android or MacCatalyst), this should be done in code.
/// </para>
/// <para>
/// On other platforms you can set this in code and it is also possible to set this via an environment variable or
/// via configuration bindings (e.g. in an app.config or an appsettings.json file).
/// </para>
/// </summary>
#if !CI_BUILD
#error Sign up for a free Sentry Account and enter your DSN here
#endif
public const string Dsn = "ENTER_YOUR_DSN_HERE";
#endif
}
12 changes: 11 additions & 1 deletion samples/Sentry.Samples.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ protected override void OnCreate(Bundle? savedInstanceState)
{
SentrySdk.Init(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// You must specify a DSN. On mobile platforms, this should be done in code here.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just be overwritten by the one below. I don't follow, why do we do this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not overwritten no... this code gets compiled only #if !SENTRY_DSN_DEFINED_IN_ENV - so when the SENTRY_DSN environment variable isn't set. This will typically be the case for SDK users who have forked the repo.

If the environment variable is set then we write it into EnvironmentVariables.Dsn where it's available to mobile samples (like this one)... which is where we read it from below. This is probably what will happen when SDK maintainers are running the samples.

#else
// To make things easier for the SDK maintainers our samples check for a SENTRY_DSN environment variable
// and write this (as a constant) into an EnvironmentVariables class. Generally, you won't want to do
// this in your own mobile projects though - you should set the DSN in code as above
options.Dsn = EnvironmentVariables.Dsn;
#endif

options.SendDefaultPii = true; // adds the user's IP address automatically

// Android specific .NET features are under the Android properties:
Expand Down
7 changes: 5 additions & 2 deletions samples/Sentry.Samples.AspNetCore.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

builder.WebHost.UseSentry(options =>
{
// A DSN is required. You can set it here, or in configuration, or in an environment variable.
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif

// Enable Sentry performance monitoring
options.TracesSampleRate = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
builder.Services.AddServerSideBlazor();
builder.WebHost.UseSentry(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.TracesSampleRate = 1.0;
options.Debug = true;
});
Expand Down
6 changes: 5 additions & 1 deletion samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.UseSentry(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.Debug = true;
});

Expand Down
7 changes: 7 additions & 0 deletions samples/Sentry.Samples.AspNetCore.Grpc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static IWebHost BuildWebHost(string[] args) =>
builder.AddGrpc();
builder.AddSentryOptions(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, via the SENTRY_DSN environment variable or in your
// appsettings.json file.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
options.Dsn = SamplesShared.Dsn;
#endif

// The parameter 'options' here has values populated through the configuration system.
// That includes 'appsettings.json', environment variables and anything else
// defined on the ConfigurationBuilder.
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.AspNetCore.Grpc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All Sentry settings can also be configured via code or environment variables:
"Sentry": {
// The DSN can also be set via environment variable
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
// Opt-in for payload submission
"MaxRequestBodySize": "Always",
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default.
Expand Down
7 changes: 7 additions & 0 deletions samples/Sentry.Samples.AspNetCore.Mvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
// See: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0
builder.WebHost.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, via the SENTRY_DSN environment variable or in your
// appsettings.json file.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
options.Dsn = SamplesShared.Dsn;
#endif

// Tracks the release which sent the event and enables more features: https://docs.sentry.io/learn/releases/
// If not explicitly set here, the SDK attempts to read it from: AssemblyInformationalVersionAttribute and AssemblyVersion
// TeamCity: %build.vcs.number%, VSTS: BUILD_SOURCEVERSION, Travis-CI: TRAVIS_COMMIT, AppVeyor: APPVEYOR_REPO_COMMIT, CircleCI: CIRCLE_SHA1
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.AspNetCore.Mvc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All Sentry settings can also be configured via code or environment variables:
"Sentry": {
// The DSN can also be set via environment variable
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
// Opt-in for payload submission
"MaxRequestBodySize": "Always",
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default.
Expand Down
8 changes: 6 additions & 2 deletions samples/Sentry.Samples.AspNetCore.Serilog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ public static WebApplication BuildWebApp(string[] args)
}));

// Add Sentry integration
// It can be defined via configuration (including `appsettings.json`, as we do here)
// It can be defined via configuration (including `appsettings.json`)
// or coded explicitly, via parameter like:
// .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...)
builder.WebHost.UseSentry();
#if !SENTRY_DSN_DEFINED_IN_ENV
builder.WebHost.UseSentry(SamplesShared.Dsn);
#else
builder.WebHost.UseSentry(EnvironmentVariables.Dsn);
#endif

// The App:
var webApplication = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// All Sentry settings can also be configured via code or environment variables:
"Sentry": {
// The DSN can also be set via environment variable
"Dsn": "https://b887218a80114d26a9b1a51c5f88e0b4@o447951.ingest.sentry.io/6601807",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
// Enable Sentry tracing features
"EnableTracing": true,
// Opt-in for payload submission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

builder.WebHost.UseSentry(o =>
{
o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
o.Dsn = SamplesShared.Dsn;
#endif
o.AddProfilingIntegration();
o.ProfilesSampleRate = 0.1;
o.TracesSampleRate = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFu
protected override void Init(IWebHostBuilder builder)
{
builder
// Add Sentry (configuration was done via appsettings.json but could be done programatically):
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set it here in code, via the SENTRY_DSN environment variable or in your
// appsettings.json file.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
.UseSentry(SamplesShared.Dsn)
#else
.UseSentry()
#endif
// Add Sentry (configuration was done via appsettings.json but could be done programatically):
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"Sentry": {
"NOTE1": "Add your own DSN below see the sample event in YOUR Sentry dashboard.",
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
"EnableTracing": true,

"NOTE2": "Many Sentry ASP.NET Core options are demonstrated in the other samples in this repository.",
Expand Down
5 changes: 4 additions & 1 deletion samples/Sentry.Samples.Azure.Functions.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
{
builder.UseSentry(host, options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
options.Dsn = SamplesShared.Dsn;
#endif
options.TracesSampleRate = 1.0;
options.Debug = true;
});
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// Initialize the Sentry SDK. (It is not necessary to dispose it.)
SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.Console.Customized/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ await SentrySdk.ConfigureScopeAsync(async scope =>
// Enable the SDK
using (SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

// Send stack trace for events that were not created from an exception
// e.g: CaptureMessage, log.LogDebug, log.LogInformation ...
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.Console.HeapDump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// Initialize the Sentry SDK. (It is not necessary to dispose it.)
SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.Console.Native/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
// Initialize the Sentry SDK. (It is not necessary to dispose it.)
SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.Console.Profiling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ private static void Main()
// Enable the SDK
using (SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

options.Debug = true;
// options.AutoSessionTracking = true;
Expand Down
6 changes: 4 additions & 2 deletions samples/Sentry.Samples.EntityFramework/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

using var _ = SentrySdk.Init(options =>
{
// You can set here in code, or you can set it in the SENTRY_DSN environment variable.
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Dsn = SamplesShared.Dsn;
#endif

options.Debug = true; // To see SDK logs on the console
options.TracesSampleRate = 1.0;
Expand Down
8 changes: 8 additions & 0 deletions samples/Sentry.Samples.GenericHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
var builder = Host.CreateApplicationBuilder();

builder.Logging.AddConfiguration(builder.Configuration);

#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set it here in code, via the SENTRY_DSN environment variable or in your
// appsettings.json file.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
builder.Logging.AddSentry(SamplesShared.Dsn);
#else
builder.Logging.AddSentry();
#endif

builder.Services.AddHostedService<SampleHostedService>();

Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.GenericHost/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}
},
"Sentry": {
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"SendDefaultPii": true // Send user name and machine name
Expand Down
4 changes: 4 additions & 0 deletions samples/Sentry.Samples.Google.Cloud.Functions/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

// *********************************************************************************************
// NOTE: to run this sample you'll need to specify a Sentry DSN, either by configuring it in the
// appsettings.json file or by setting the SENTRY_DSN environment variable.
// *********************************************************************************************
[assembly: FunctionsStartup(typeof(SentryStartup))]

public class Function : IHttpFunction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Sentry": {
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
"MaxRequestBodySize": "Always",
"SendDefaultPii": true,
"EnableTracing": true
Expand Down
Loading
Loading