# App

{% code lineNumbers="true" fullWidth="true" %}

```csharp
    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;
        };
    }
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://engine3d.gitbook.io/wiki/editor/app.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
