Logging username with Elmah for WCF Webservices
We are using the approach described here to log our webservice errors with Elmah. And this actually works, but sadly the username beeing logged is empty.
We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User
has the correct User set.
We also tried:
HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));
and
ErrorLog.GetDefault(null).Log(new Error(pError));
Without success.
Any ideas on how we can make Elmah log the username?
On a sidenote, when logging the error directly within the Webservice, the username is logged as expected. But taking this approach is not very DRY.
asp.net wcf exception logging elmah
add a comment |
We are using the approach described here to log our webservice errors with Elmah. And this actually works, but sadly the username beeing logged is empty.
We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User
has the correct User set.
We also tried:
HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));
and
ErrorLog.GetDefault(null).Log(new Error(pError));
Without success.
Any ideas on how we can make Elmah log the username?
On a sidenote, when logging the error directly within the Webservice, the username is logged as expected. But taking this approach is not very DRY.
asp.net wcf exception logging elmah
add a comment |
We are using the approach described here to log our webservice errors with Elmah. And this actually works, but sadly the username beeing logged is empty.
We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User
has the correct User set.
We also tried:
HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));
and
ErrorLog.GetDefault(null).Log(new Error(pError));
Without success.
Any ideas on how we can make Elmah log the username?
On a sidenote, when logging the error directly within the Webservice, the username is logged as expected. But taking this approach is not very DRY.
asp.net wcf exception logging elmah
We are using the approach described here to log our webservice errors with Elmah. And this actually works, but sadly the username beeing logged is empty.
We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User
has the correct User set.
We also tried:
HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));
and
ErrorLog.GetDefault(null).Log(new Error(pError));
Without success.
Any ideas on how we can make Elmah log the username?
On a sidenote, when logging the error directly within the Webservice, the username is logged as expected. But taking this approach is not very DRY.
asp.net wcf exception logging elmah
asp.net wcf exception logging elmah
edited May 23 '17 at 12:25
Community♦
11
11
asked Oct 12 '10 at 7:17
ThomasThomas
1,0451934
1,0451934
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
add a comment |
This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.
In your case, setting the User
property on all errors, can be achieved by adding the ErrorLog_Filtering
-method:
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
var httpContext = args.Context as HttpContext;
if (httpContext != null)
{
var error = new Error(args.Exception, httpContext);
error.User = httpContext.User.Identity.Name;
ErrorLog.GetDefault(httpContext).Log(error);
args.Dismiss();
}
}
add a comment |
As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.
public static void LogError(Exception exception, string username)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string {});
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3912383%2flogging-username-with-elmah-for-wcf-webservices%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
add a comment |
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
add a comment |
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.
answered Oct 12 '10 at 7:37
Shay ErlichmenShay Erlichmen
28.5k65882
28.5k65882
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
add a comment |
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
– Thomas
Oct 12 '10 at 7:56
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
Yes, you don't want to mess with thread principal
– Shay Erlichmen
Oct 12 '10 at 7:59
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
And if I Set it back to the old Principal after logging the error with elmah?
– Thomas
Oct 12 '10 at 8:00
1
1
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
should work, use, try..finally, but its a hack...
– Shay Erlichmen
Oct 12 '10 at 8:01
add a comment |
This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.
In your case, setting the User
property on all errors, can be achieved by adding the ErrorLog_Filtering
-method:
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
var httpContext = args.Context as HttpContext;
if (httpContext != null)
{
var error = new Error(args.Exception, httpContext);
error.User = httpContext.User.Identity.Name;
ErrorLog.GetDefault(httpContext).Log(error);
args.Dismiss();
}
}
add a comment |
This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.
In your case, setting the User
property on all errors, can be achieved by adding the ErrorLog_Filtering
-method:
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
var httpContext = args.Context as HttpContext;
if (httpContext != null)
{
var error = new Error(args.Exception, httpContext);
error.User = httpContext.User.Identity.Name;
ErrorLog.GetDefault(httpContext).Log(error);
args.Dismiss();
}
}
add a comment |
This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.
In your case, setting the User
property on all errors, can be achieved by adding the ErrorLog_Filtering
-method:
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
var httpContext = args.Context as HttpContext;
if (httpContext != null)
{
var error = new Error(args.Exception, httpContext);
error.User = httpContext.User.Identity.Name;
ErrorLog.GetDefault(httpContext).Log(error);
args.Dismiss();
}
}
This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.
In your case, setting the User
property on all errors, can be achieved by adding the ErrorLog_Filtering
-method:
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
var httpContext = args.Context as HttpContext;
if (httpContext != null)
{
var error = new Error(args.Exception, httpContext);
error.User = httpContext.User.Identity.Name;
ErrorLog.GetDefault(httpContext).Log(error);
args.Dismiss();
}
}
answered Feb 21 '17 at 7:49
ThomasArdalThomasArdal
2,93721435
2,93721435
add a comment |
add a comment |
As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.
public static void LogError(Exception exception, string username)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string {});
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
add a comment |
As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.
public static void LogError(Exception exception, string username)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string {});
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
add a comment |
As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.
public static void LogError(Exception exception, string username)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string {});
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.
public static void LogError(Exception exception, string username)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string {});
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
answered Nov 20 '18 at 10:29
jdimkojdimko
608
608
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3912383%2flogging-username-with-elmah-for-wcf-webservices%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown