0

I build a C# ASP.NET Core 2 MVC app and this is working fine. Sometimes I got some strange errors in my database and I need more data to analyze.

I have my own error-class

if (env.IsProduction()) 
{
      app.UseBrowserLink(); 
      app.UseExceptionHandler("/error/index"); 
}

And log already more data - like IP address or userid if user is logged in:

_context.Error.Add(new Error
    {
        errorMessage = feature.Error.Message?.ToString(),
        errorStackTrace = feature.Error.StackTrace?.ToString(),
        errorUserAgent = Request.Headers["User-Agent"].ToString(),
        errordate = DateTime.Now,
        errorIP = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString(),
        errorInnerException = feature.Error.InnerException?.ToString(),
        errorSource = feature.Error.Source?.ToString(),
        errorUserID = (tmpUserID.ToString() == "00000000-0000-0000-0000-000000000000") ? null : tmpUserID.ToString(),
        errorForm = SesionFormData
    });
_context.SaveChanges();

But I found no way to get session-data or even simple URL-parameter of the request who crashed :(

Is there a way?

Thanks a lot Ralf

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ralf
  • 269
  • 1
  • 3
  • 13
  • 1
    What about `HttpContext.Request.QueryString` for url-parameter and `HttpContext.Session` for session? – Edward Feb 25 '19 at 06:34
  • Relying on error handling exclusively is naive. Add logging to your application, and log what's actually happening when it's happening. In other words, if you're getting errors from a database query, log that when you're making the query. – Chris Pratt Feb 25 '19 at 14:31
  • @Tao Zhou I've tried it, but both don't work out. Querystring does not contain the URL. In Debug I can find it in "HttpContext.Features.Microsoft.AspNetCore.Http.Features.IHttpRequestFeature.RawTarget" but I cant use this for the variable :( In HttpContext.Session.Keys I can find the keys but not the values. I'll keep looking. Thought someone had done something like this for Core, but couldn't find an example anywhere. – Ralf Feb 27 '19 at 20:10

1 Answers1

0

You can get the raw url in this way:

var requestFeature = HttpContext.Features.Get<IHttpRequestFeature>();
var originalUrl = requestFeature.RawTarget;

For details, see comments to this answer: https://stackoverflow.com/a/38747631/765818

jeanie77
  • 515
  • 1
  • 4
  • 22