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

3
  • 1
    What about HttpContext.Request.QueryString for url-parameter and HttpContext.Session for session? Commented Feb 25, 2019 at 6: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. Commented Feb 25, 2019 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. Commented Feb 27, 2019 at 20:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.