Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental wpf integration #1036

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions samples/Sentry.Samples.Wpf/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Sentry.Samples.Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sentry.Samples.Wpf"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
81 changes: 81 additions & 0 deletions samples/Sentry.Samples.Wpf/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;
using Sentry.Protocol;

namespace Sentry.Samples.Wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
// TODO: Should be part of Sentry.Wpf and hook automatically
DispatcherUnhandledException += (sender, e) =>
{
if (e.Exception is Exception ex)
{
ex.Data[Mechanism.HandledKey] = e.Handled;
ex.Data[Mechanism.MechanismKey] = "App.DispatcherUnhandledException";
_ = SentrySdk.CaptureException(ex);
}

if (!e.Handled)
{
// Unhandled will crash the app so flush the queue:
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult();
}
};

SentrySdk.Init(o =>
{
o.Dsn = "https://[email protected]/5428537";
// TODO: Should print to VS debug window (similar to Sentry for ASP.NET)
o.Debug = true;
// TODO: Doesn't support multiple instances of the process on the same directory yet
o.CacheDirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
// For testing, set to 100% transactions for performance monitoring.
o.TracesSampleRate = 1.0;
});
}

protected override void OnStartup(StartupEventArgs e)
{
SentrySdk.AddBreadcrumb("OnStartup", "app.lifecycle");
SentrySdk.ConfigureScope(s => s.Transaction = SentrySdk.StartTransaction("Startup", "app.start"));
base.OnStartup(e);
}

protected override void OnNavigating(NavigatingCancelEventArgs e)
{
SentrySdk.AddBreadcrumb("NavigatingCancelEventArgs",
"navigation",
data: new Dictionary<string, string>
{
{"url", e.Uri.ToString()}
});
base.OnNavigating(e);
}

protected override void OnFragmentNavigation(FragmentNavigationEventArgs e)
{
SentrySdk.AddBreadcrumb("OnFragmentNavigation",
"navigation",
data: new Dictionary<string, string>
{
{"fragment", e.Fragment},
{"handled", e.Handled.ToString()}
});
base.OnFragmentNavigation(e);
}

protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
SentrySdk.Close();
}
}
}
10 changes: 10 additions & 0 deletions samples/Sentry.Samples.Wpf/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
12 changes: 12 additions & 0 deletions samples/Sentry.Samples.Wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window x:Class="Sentry.Samples.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Sentry.Samples.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Click="ButtonBase_OnClick"></Button>
</Grid>
</Window>
32 changes: 32 additions & 0 deletions samples/Sentry.Samples.Wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Windows;

namespace Sentry.Samples.Wpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();

private ISpan _initSpan;

public override void BeginInit()
{
_initSpan = SentrySdk.GetSpan()?.StartChild("BeginInit");
base.BeginInit();
}

public override void EndInit()
{
base.EndInit();
_initSpan?.Finish();
// Is this the API to close the current transaction bound to the scope? Maybe we need to revisit this..
SentrySdk.ConfigureScope(s => s.Transaction?.Finish());
}

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
=> throw new InvalidOperationException("This button shall not be pressed!");
}
}
13 changes: 13 additions & 0 deletions samples/Sentry.Samples.Wpf/Sentry.Samples.Wpf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
</ItemGroup>

</Project>