tareqimbasher / NetPad

A cross-platform C# editor and playground.
MIT License
1.09k stars 56 forks source link

Running ASP.NET Core code on a Windows PC takes a long time (hang) or results in errors #188

Closed rkttu closed 2 months ago

rkttu commented 3 months ago

When you write and run code using a code snippet called a webapp, unlike the macOS version, it takes a long time on Windows or fails to run with the following error messages

I think it's a process file occupancy issue, so I shut down and restart the NetPad, but that doesn't solve the problem. (After restarting the Windows, the problem remains the same.)

I don't know how to provide debugging information, so I'm just sharing what I'm seeing. If there is a way to get a detailed log, please let me know and I'll follow up.

System.UnauthorizedAccessException: Access to the path 'NetPad.ExternalProcessScriptRuntime.Interface.dll' is denied.
   at System.IO.FileSystem.RemoveDirectoryRecursive(String fullPath, WIN32_FIND_DATA& findData, Boolean topLevel)
   at System.IO.FileSystem.RemoveDirectory(String fullPath, Boolean recursive)
   at System.IO.DirectoryInfo.Delete(Boolean recursive)
   at NetPad.Runtimes.ExternalProcessScriptRuntime.SetupExternalProcessRootDirectoryAsync(RunDependencies runDependencies) in /home/tips/Source/TIPS/NetPad/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.Setup.cs:line 208
   at NetPad.Runtimes.ExternalProcessScriptRuntime.RunScriptAsync(RunOptions runOptions) in /home/tips/Source/TIPS/NetPad/src/Core/ScriptRuntimes/NetPad.ExternalProcessScriptRuntime/ExternalProcessScriptRuntime.cs:line 86
스크린샷 2024-04-07 오후 8 39 04
rkttu commented 3 months ago
스크린샷 2024-04-07 오후 8 42 16

For reference, here are the current dependency check results.

tareqimbasher commented 2 months ago

@rkttu I'm investigating this issue, and from what I'm seeing so far, on Windows specifically, the ASP.NET app runs but the output pane doesn't show any logs so you might feel like it has hung. But if you go to http://localhost:5678 (url from your example) in a web browser you should see the default welcome page. Please let me know if that's not what you're seeing.

I'm still digging deeper to understand why logs aren't showing up. I haven't encountered the UnauthorizedAccessException error you described yet. Do you get the error randomly? In other words, is it seemingly random when you get the error vs when it runs without logs?

tareqimbasher commented 2 months ago

There is definitely something special about how the default ASP.NET Core logging provider works on Windows vs Linux/macOS. If you use the following code that implements a custom logger that explicitly logs to the Console, you will see the logs:

var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
builder.Logging.AddProvider(new LoggerProvider());

var app = builder.Build();
app.UseWelcomePage();
await app.RunAsync("http://localhost:5678");

public class CustomLogger(string categoryName) : ILogger
{
    private readonly string CategoryName = categoryName;

    public IDisposable BeginScope<TState>(TState state)
    {
        return new NoopDisposable();
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        string message = "";
        if (formatter != null)
        {
            message += formatter(state, exception);
        }
        Console.WriteLine($"[{logLevel.ToString()[0..3].ToUpper()}] {CategoryName}: {message}");
    }

    private class NoopDisposable : IDisposable
    {
        public void Dispose()
        {
        }
    }
}

public class LoggerProvider : ILoggerProvider
{
    public ILogger CreateLogger(string categoryName)
    {
        return new CustomLogger(categoryName);
    }

    public void Dispose()
    {
    }
}

Still gathering more info about what they are doing internally that's different on Windows. From what I can tell so far, if ASP.NET detects that standard output is redirected the built-in logging provider(s) don't forward output to the redirected output, and instead direct it somewhere else, or maybe no where at all. I can't find the output in the Event Viewer either so its not being redirected there.

tareqimbasher commented 2 months ago

Inspecting the ConsoleLoggingProvider source code confirms my suspicion. Working on a fix for next release.