Closed zgredas closed 1 year ago
@zgredas as a short term work around you could populate the request info object from the IOwinContext and populate it like this: ex.ToExceptionless().AddObject(Event.KnownDataKeys.RequestInfo, GetRequestInfo(context.Request)).Submit();
Would be a huge help if you could send a snippet of the request info being populated from the IOwinRequest
. We could then work this into the client.
@niemyjski please take a look on the following code
public static RequestInfo ToRequestInfo(this IOwinRequest request, IEnumerable<string> exclusions = null)
{
if (request == null)
return null;
var info = new RequestInfo
{
ClientIpAddress = request.LocalIpAddress,
HttpMethod = request.Method
};
if (request.Headers["User-Agent"] != null)
info.UserAgent = request.Headers["User-Agent"];
if (request.Uri != null)
{
info.Host = request.Uri.Host;
info.IsSecure = request.Uri.Scheme == "https";
info.Path = IsNullOrEmpty(request.Uri.LocalPath) ? "/" : request.Uri.LocalPath;
info.Port = request.Uri.Port;
}
if (request.Headers["Referrer"] != null)
info.Referrer = request.Headers["Referrer"];
if (exclusions == null) exclusions = new List<string>();
var exclusionList = exclusions as string[] ?? exclusions.ToArray();
info.Cookies = request.Cookies.ToDictionary(exclusionList);
info.QueryString = request.Uri.ParseQueryString().ToDictionary(exclusionList);
return info;
}
Which value should I use for ClientIpAddress
?
Is it request.LocalIpAddress
or request.RemoteIpAddress
?
It should be the clients remote ip address
@niemyjski ok, so it leaves us with the following
public static RequestInfo ToRequestInfo(this IOwinRequest request, IEnumerable<string> exclusions = null)
{
if (request == null)
return null;
var info = new RequestInfo
{
ClientIpAddress = request.RemoteIpAddress,
HttpMethod = request.Method
};
if (request.Headers["User-Agent"] != null)
info.UserAgent = request.Headers["User-Agent"];
if (request.Uri != null)
{
info.Host = request.Uri.Host;
info.IsSecure = request.Uri.Scheme == "https";
info.Path = IsNullOrEmpty(request.Uri.LocalPath) ? "/" : request.Uri.LocalPath;
info.Port = request.Uri.Port;
}
if (request.Headers["Referrer"] != null)
info.Referrer = request.Headers["Referrer"];
if (exclusions == null) exclusions = new List<string>();
var exclusionList = exclusions as string[] ?? exclusions.ToArray();
info.Cookies = request.Cookies.ToDictionary(exclusionList);
info.QueryString = request.Uri.ParseQueryString().ToDictionary(exclusionList);
return info;
}
private static readonly List<string> _ignoredFormFields = new List<string> {
"__*"
};
private static readonly List<string> _ignoredCookies = new List<string> {
".ASPX*",
"__*",
"*SessionId*"
};
public static Dictionary<string, string> ToDictionary(this NameValueCollection values, IEnumerable<string> exclusions)
{
var d = new Dictionary<string, string>();
var patternsToMatch = exclusions as string[] ?? exclusions.ToArray();
foreach (string key in values.AllKeys)
{
if (IsNullOrEmpty(key) || key.AnyWildcardMatches(_ignoredFormFields, true) || key.AnyWildcardMatches(patternsToMatch, true))
continue;
try
{
string value = values.Get(key);
d.Add(key, value);
}
catch (Exception ex)
{
if (!d.ContainsKey(key))
d.Add(key, ex.Message);
}
}
return d;
}
public static Dictionary<string, string> ToDictionary(this RequestCookieCollection cookies, IEnumerable<string> exclusions)
{
var d = new Dictionary<string, string>();
foreach (var cookie in cookies.Where(c => !IsNullOrEmpty(c.Key) && !c.Key.AnyWildcardMatches(_ignoredCookies, true) && !c.Key.AnyWildcardMatches(exclusions, true)))
{
if (!d.ContainsKey(cookie.Key))
d.Add(cookie.Key, cookie.Value);
}
return d;
}
https://github.com/exceptionless/Exceptionless.Net/issues/85 cc @luisvsilva
@zgredas would you mind submitting a pr for this
I'm closing this as we did some follow up work in #85. Can you please include a pr for any missing items.
"it’s currently not possible to add the request info via middleware in the current form as we expect an HttpActionContext and use the request object from that.. I almost think that the web api plugin should be refactored a little bit to be a bit more generic and maybe provide overloads for both OwinContext and HttpActionContext as well as helpers for specifying it on the event builder.
One work around for the time being would be to add an extension method to populate it from an OwinContext"