FileServer .net core logging and force download

150 views Asked by At

I'm currently trying to write a simple file server to server file for another webapi or services.

I have one mandatory requirements, I need to get every request/download log in a file. And also on the basic fileserver middleware broad by Microsoft, you don't download the image for example. It's just showing the image. Is there a way to force download and also change the logging behavior ? I tried to put a middleware to log requests, but is only log when there is directory changement.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddDirectoryBrowser();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
        app.UseDeveloperExceptionPage();
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseFileServer(new FileServerOptions
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "MyStaticFiles")),
        RequestPath = "/StaticFiles",
        EnableDirectoryBrowsing = true
    });

    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
    });
}
0

There are 0 answers