KP.ExceptionHandling 1.0.1

.NET 5.0
There is a newer version of this package available.
See the version list below for details.
Install-Package KP.ExceptionHandling -Version 1.0.1
dotnet add package KP.ExceptionHandling --version 1.0.1
<PackageReference Include="KP.ExceptionHandling" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KP.ExceptionHandling --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: KP.ExceptionHandling, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install KP.ExceptionHandling as a Cake Addin
#addin nuget:?package=KP.ExceptionHandling&version=1.0.1

// Install KP.ExceptionHandling as a Cake Tool
#tool nuget:?package=KP.ExceptionHandling&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

About

Simple code sample for application-wide exception handler to write unhandled exceptions to a log file.

Prerequisites

To run this project as is in this repository.

  • VS2019
  • .NET Core 5

TechNet Wiki article

Unhandled runtime exceptions window forms (C#)

Implement

using System;
using System.Windows.Forms;
using ExceptionHandling;

namespace ThreadExceptionWindowsFormsApp
{
    /// <summary>
    /// Rig for unhandled exceptions.
    /// Note Text property set on error form is for demo purposes only
    /// </summary>
    static class Program
    {

        [STAThread]
        static void Main()
        {
            
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Handling UI thread exceptions to the event.
            Application.ThreadException += UnhandledExceptions.Application_ThreadException;

            // For handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptions.CurrentDomain_UnhandledException;

            // Indicates capturing exception has completed
            UnhandledExceptions.OnProcessingCompletedEvent += OnProcessingCompletedEvent;


            Application.Run(new Form1());

        }
        /// <summary>
        /// Display window informing application most close
        /// </summary>
        private static void OnProcessingCompletedEvent()
        {
            var f = new AppErrorForm { Text = @"Your title goes here" };
            f.ShowDialog();
            Application.Exit();
        }
    }
}

Source code

Source code for both the class project and test application.

Get source without all projects

With Git installed, create a batch file with the following contents, save, run and add the cloned projects to your solution.

mkdir code
cd code
git init
git remote add -f origin https://github.com/karenpayneoregon/csharp-features
git sparse-checkout init --cone
git sparse-checkout add ExceptionHandling
git sparse-checkout add ThreadExceptionWindowsFormsApp
git pull origin master
:clean-up
del .gitattributes
del .gitignore
del .yml
del .editorconfig
del *.md
del *.sln
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 224 5/23/2021
1.0.1 191 2/4/2021
1.0.0 177 2/3/2021

C#, .NET5, Desktop,Exception handling