Skip to content

Commit 40432c9

Browse files
committed
Made it easier to capture the HttpActionContext so request and user info is captured by default.
1 parent 31ef359 commit 40432c9

File tree

6 files changed

+36
-26
lines changed

6 files changed

+36
-26
lines changed

samples/Exceptionless.SampleWebApi/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</dependentAssembly>
2828
<dependentAssembly>
2929
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
30-
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
30+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
3131
</dependentAssembly>
3232
</assemblyBinding>
3333
</runtime>

samples/Exceptionless.SampleWebApi/Controllers/ValuesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Exceptionless.SampleWebApi.Controllers {
77
public class ValuesController : ApiController {
88
// GET api/values
99
public IEnumerable<string> Get() {
10-
ExceptionlessClient.Default.SubmitLog("ValuesController", "Getting results", LogLevel.Info);
10+
ExceptionlessClient.Default.CreateLog("ValuesController", "Getting results", LogLevel.Info).SetHttpActionContext(ActionContext).Submit();
1111
throw new ApplicationException("WebApi GET error");
1212
}
1313

samples/Exceptionless.SampleWebApi/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void Main(string[] args) {
2626
Console.WriteLine("Press any key to send a request...");
2727
ConsoleKeyInfo key = Console.ReadKey();
2828
while (key.KeyChar != 27) {
29-
// Create HttpCient and make a request to api/values
29+
// Create HttpCient and make a request to api/values
3030
var client = new HttpClient();
3131
HttpResponseMessage response = client.GetAsync(baseAddress + "api/values").GetAwaiter().GetResult();
3232

src/Platforms/Exceptionless.Web/RequestInfoCollector.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,11 @@ private static Dictionary<string, string> ToDictionary(this NameValueCollection
127127
return d;
128128
}
129129

130-
private static string GetUserIPAddress(HttpContextBase context)
131-
{
130+
private static string GetUserIPAddress(HttpContextBase context) {
132131
string clientIp = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
133-
if (string.IsNullOrEmpty(clientIp))
134-
{
132+
if (String.IsNullOrEmpty(clientIp))
135133
clientIp = context.Request.UserHostAddress;
136-
}
134+
137135
return clientIp;
138136
}
139137
}

src/Platforms/Exceptionless.WebApi/ExceptionlessWebApiExtensions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
namespace Exceptionless {
1515
public static class ExceptionlessWebApiExtensions {
1616
/// <summary>
17-
/// Reads configuration settings, configures various plugins and wires up to platform specific exception handlers.
17+
/// Reads configuration settings, configures various plugins and wires up to platform specific exception handlers.
1818
/// </summary>
1919
/// <param name="client">The ExceptionlessClient.</param>
20-
/// <param name="config">The HttpConfiguration instance.</param>
20+
/// <param name="config">The HttpConfiguration instance.</param>
2121
public static void RegisterWebApi(this ExceptionlessClient client, HttpConfiguration config) {
2222
if (client == null)
2323
throw new ArgumentNullException(nameof(client));
2424

2525
client.Startup();
2626
client.Configuration.AddPlugin<ExceptionlessWebApiPlugin>();
2727
client.Configuration.AddPlugin<IgnoreUserAgentPlugin>();
28-
28+
2929
config.Services.Add(typeof(IExceptionLogger), new ExceptionlessExceptionLogger());
3030

3131
ReplaceHttpErrorHandler(config, client);
@@ -91,8 +91,13 @@ public static Event AddHttpRequestInfo(this Event ev, HttpActionContext context,
9191
/// </summary>
9292
/// <param name="builder">The event builder.</param>
9393
/// <param name="context">The http action context to gather information from.</param>
94+
[Obsolete("Please call SetHttpActionContext instead.", true)]
9495
public static EventBuilder AddHttpRequestInfo(this EventBuilder builder, HttpActionContext context) {
95-
builder.Target.AddHttpRequestInfo(context, builder.Client.Configuration);
96+
return builder.SetHttpActionContext(context);
97+
}
98+
99+
public static EventBuilder SetHttpActionContext(this EventBuilder builder, HttpActionContext context) {
100+
builder.PluginContextData["HttpActionContext"] = context;
96101
return builder;
97102
}
98103

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
-------------------------------------
2-
Exceptionless Readme
2+
Exceptionless Readme
33
-------------------------------------
4-
Exceptionless provides real-time error reporting for your apps. It organizes the
5-
gathered information into simple actionable data that will help your app become
4+
Exceptionless provides real-time error reporting for your apps. It organizes the
5+
gathered information into simple actionable data that will help your app become
66
exceptionless!
77

88
Learn more at http://exceptionless.io.
99

1010
-------------------------------------
11-
How to get an api key
11+
How to get an api key
1212
-------------------------------------
13-
The Exceptionless client requires an api key to use the Exceptionless service.
14-
You can get your Exceptionless api key by logging into http://exceptionless.io
13+
The Exceptionless client requires an api key to use the Exceptionless service.
14+
You can get your Exceptionless api key by logging into http://exceptionless.io
1515
and viewing your project configuration page.
1616

1717
-------------------------------------
18-
ASP.NET Web Api Integration
18+
ASP.NET Web Api Integration
1919
-------------------------------------
20-
The Exceptionless.WebApi package will automatically configure your web.config.
21-
All you need to do is open the web.config and add your Exceptionless api key to
20+
The Exceptionless.WebApi package will automatically configure your web.config.
21+
All you need to do is open the web.config and add your Exceptionless api key to
2222
the web.config Exceptionless section.
2323

2424
<exceptionless apiKey="API_KEY_HERE" />
2525

2626
Next, you must import the "Exceptionless" namespace and call the following line
27-
of code to start reporting unhandled exceptions. You will need to run code during
27+
of code to start reporting unhandled exceptions. You will need to run code during
2828
application startup and pass it an HttpConfiguration instance. Please note that this
2929
code is normally placed inside of the WebApiConfig classes Register method.
3030

@@ -40,13 +40,20 @@ for examples on sending events to Exceptionless.
4040
-------------------------------------
4141
Manually reporting an exception
4242
-------------------------------------
43-
By default the Exceptionless Client will report all unhandled exceptions. You can
44-
also manually send an exception by importing the Exceptionless namespace and calling
43+
By default the Exceptionless Client will report all unhandled exceptions. You can
44+
also manually send an exception by importing the Exceptionless namespace and calling
4545
the following method.
4646

4747
exception.ToExceptionless().Submit()
4848

49+
Please note that Web Api doesn't have a static http context. If possible, it is recommended
50+
that you set the HttpActionContext when submitting events. Doing so will allow the request and
51+
user information to be populated. You can do this by calling the SetHttpActionContext EventBuilder
52+
extension method.
53+
54+
exception.ToExceptionless().SetHttpActionContext(ActionContext).Submit()
55+
4956
-------------------------------------
50-
Documentation and Support
57+
Documentation and Support
5158
-------------------------------------
52-
Please visit http://exceptionless.io for documentation and support.
59+
Please visit http://exceptionless.io for documentation and support.

0 commit comments

Comments
 (0)