Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically record the start and end of an MVC FileResult for logging of incomplete downloads?

I have been investigating how My ASP.NET MVC application can log unsuccessful / incomplete downloads. I have a controller handling the file requests which currently returns a FileResult. What I need to record is the IPAddress, filename and when the download started, then the same data and when it completed. I have looked at intercepting the request start and end with an HttpModule and also IIS7 failed request tracing but am not sure which route would be best and have this feeling that maybe I am missing an obvious answer to the problem.

Does anyone have any suggestions or know of any alternatives as this seems like something many people would want to know from their web server?

Thanks for your help

like image 577
Richard Avatar asked Dec 22 '25 07:12

Richard


1 Answers

You could try writing a custom FilePathResult and override the WriteFileMethod:

public class CustomFileResult : FilePathResult
{
    public CustomFileResult(string fileName, string contentType)
        : base(fileName, contentType)
    { }

    protected override void WriteFile(HttpResponseBase response)
    {
        // TODO: Record file download start
        base.WriteFile(response);
        // TODO: Record file download end
    }
}

and in your controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return new CustomFileResult(@"d:\test.jpg", "image/jpg");
    }
}
like image 119
Darin Dimitrov Avatar answered Dec 24 '25 06:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!