Skip to main content
added 1050 characters in body
Source Link
NaolShow
  • 23
  • 1
  • 5
using System;
using System.IO;
using System.Windows;
using WPFTemplate.Classes;
using WPFTemplate.Classes.Core;

namespace WPFTemplate {

    public partial class App : Application {

        /// <summary>
        /// Logger
        /// </summary>
        private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Application starting point
        /// </summary>
        protected override void OnStartup(StartupEventArgs e) {

            // ---- Application

            // -- Directory
            try {

                // Create application main directory
                Directory.CreateDirectory(Reference.AppPath);

            } catch (Exception ex) {

                // Log
                Log.Error(ex);

                // The path isn't valid/found (ex: with network name)
                if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException) {
                    MessageBox.Show(string.Format("The application path '{0}' isn't valid..\nPlease check if the path is valid", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // The user hasn't the permission to write (then need to run the application as administrator)
                else if (ex is UnauthorizedAccessException) {
                    MessageBox.Show(string.Format("The application hasn't the right to write data in the application path '{0}'..\nPlease start the application as administrator", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // Path is too long (max: 256 chars)
                else if (ex is PathTooLongException) {
                    MessageBox.Show(string.Format("The application path '{0}' is too long..\nPlease install the application in another place", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }

                // Shutdown
                Application.Current.Shutdown();

            }

        }

        protected override void OnExit(ExitEventArgs e) {

            // -- Logs

            Log.Info("Stopping the application");

            NLog.LogManager.Shutdown();

        }

    }

}

try {

    // Create application main directory
    Directory.CreateDirectory(Reference.AppPath);

} catch (Exception ex) {

    // Log
    Log.Error(ex);

    // The path isn't valid/found (ex: with network name)
    if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException) {
        MessageBox.Show(string.Format("The application path '{0}' isn't valid..\nPlease check if the path is valid", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }
    // The user hasn't the permission to write (then need to run the application as administrator)
    else if (ex is UnauthorizedAccessException) {
        MessageBox.Show(string.Format("The application hasn't the right to write data in the application path '{0}'..\nPlease start the application as administrator", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }
    // Path is too long (max: 256 chars)
    else if (ex is PathTooLongException) {
        MessageBox.Show(string.Format("The application path '{0}' is too long..\nPlease install the application in another place", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }

    // Shutdown
    Application.Current.Shutdown();

}
using System;
using System.IO;
using System.Windows;
using WPFTemplate.Classes;
using WPFTemplate.Classes.Core;

namespace WPFTemplate {

    public partial class App : Application {

        /// <summary>
        /// Logger
        /// </summary>
        private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Application starting point
        /// </summary>
        protected override void OnStartup(StartupEventArgs e) {

            // ---- Application

            // -- Directory
            try {

                // Create application main directory
                Directory.CreateDirectory(Reference.AppPath);

            } catch (Exception ex) {

                // Log
                Log.Error(ex);

                // The path isn't valid/found (ex: with network name)
                if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException) {
                    MessageBox.Show(string.Format("The application path '{0}' isn't valid..\nPlease check if the path is valid", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // The user hasn't the permission to write (then need to run the application as administrator)
                else if (ex is UnauthorizedAccessException) {
                    MessageBox.Show(string.Format("The application hasn't the right to write data in the application path '{0}'..\nPlease start the application as administrator", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // Path is too long (max: 256 chars)
                else if (ex is PathTooLongException) {
                    MessageBox.Show(string.Format("The application path '{0}' is too long..\nPlease install the application in another place", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }

                // Shutdown
                Application.Current.Shutdown();

            }

        }

        protected override void OnExit(ExitEventArgs e) {

            // -- Logs

            Log.Info("Stopping the application");

            NLog.LogManager.Shutdown();

        }

    }

}

Source Link
NaolShow
  • 23
  • 1
  • 5

Right way to handle, log and display exceptions

I repost my question here because it has been closed on stack overflow

This question could sound stupid but I am new to the logging, displaying and handling exceptions. Because before I was just doing a big try catch (Exception ex) to get exceptions (test softwares)

But now I am doing a "real" software, which will have a public release. And I am asking what is the right way of doing those things.

I am using WPF with the .NET Framework 4.7.2, and i'm also using NLog

For now, I did a little code, and I am just asking like a review / advices. I put that code at startup, so in the app.xaml.cs with a protected override OnStartup()

try {

    // Create application main directory
    Directory.CreateDirectory(Reference.AppPath);

} catch (Exception ex) {

    // Log
    Log.Error(ex);

    // The path isn't valid/found (ex: with network name)
    if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException) {
        MessageBox.Show(string.Format("The application path '{0}' isn't valid..\nPlease check if the path is valid", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }
    // The user hasn't the permission to write (then need to run the application as administrator)
    else if (ex is UnauthorizedAccessException) {
        MessageBox.Show(string.Format("The application hasn't the right to write data in the application path '{0}'..\nPlease start the application as administrator", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }
    // Path is too long (max: 256 chars)
    else if (ex is PathTooLongException) {
        MessageBox.Show(string.Format("The application path '{0}' is too long..\nPlease install the application in another place", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
    }

    // Shutdown
    Application.Current.Shutdown();

}

I did a custom message for every exceptions, and it's a little bit long

So i'm waiting for your reviews/advices and thanks a lot i'm still a beginner in that x)