App

    public App()
    {
        this.InitializeComponent();

        var documentsDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var rootPath = Path.Combine(documentsDir, "3DEngine");
        var logFilePath = Path.Combine(rootPath, "Application.log");

        // Create directory.
        if (!Directory.Exists(rootPath))
            Directory.CreateDirectory(rootPath);

        // Increment log if it is locked by another process.
        bool? isLocked = logFilePath.IsFileLocked();
        if (isLocked is not null)
            if (isLocked.Value)
                logFilePath = logFilePath.IncrementPathIfExists(
                    Directory.GetFiles(rootPath)
                        .Select(p => Path.GetFileNameWithoutExtension(p))
                        .ToArray());

        // Reset log.
        if (File.Exists(logFilePath))
            File.WriteAllText(logFilePath, String.Empty);

        // Set up listener.
        FileStream traceLog = new(logFilePath, FileMode.OpenOrCreate);
        TextWriterTraceListener listener = new(traceLog);

        // Pass listener to trace.
        Trace.Listeners.Add(listener);
        // Automatically write into file.
        Trace.AutoFlush = true;

        this.UnhandledException += (s, e) =>
        {
            // Write date and time.
            Debug.WriteLine($"[{DateTime.Now}]");

            // Write file name, line number, and method name.
            StackTrace stackTrace = new StackTrace(e.Exception, true);
            StackFrame frame = stackTrace.GetFrame(0); // Get the top frame (most recent method call).

            string fileName = frame.GetFileName();
            int lineNumber = frame.GetFileLineNumber();
            string methodName = frame.GetMethod().Name;

            if (fileName is not null)
                Debug.WriteLine($"{fileName}:{lineNumber} ({methodName})");

            Debug.WriteLine(e.Exception);

            if (Main.Instance is not null)
                Output.Log(e.Exception, MessageType.Error, lineNumber, methodName, fileName);

            Debug.WriteLine("\n");

            // Mark the event as handled to prevent it from being processed further.
            e.Handled = true;
        };
    }

Last updated