Logging username with Elmah for WCF Webservices












5















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.










share|improve this question





























    5















    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.










    share|improve this question



























      5












      5








      5


      3






      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:25









      Community

      11




      11










      asked Oct 12 '10 at 7:17









      ThomasThomas

      1,0451934




      1,0451934
























          3 Answers
          3






          active

          oldest

          votes


















          4














          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.






          share|improve this answer
























          • 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



















          2














          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();
          }
          }





          share|improve this answer































            0














            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);
            }





            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              4














              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.






              share|improve this answer
























              • 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
















              4














              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.






              share|improve this answer
























              • 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














              4












              4








              4







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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



















              • 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













              2














              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();
              }
              }





              share|improve this answer




























                2














                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();
                }
                }





                share|improve this answer


























                  2












                  2








                  2







                  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();
                  }
                  }





                  share|improve this answer













                  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();
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 21 '17 at 7:49









                  ThomasArdalThomasArdal

                  2,93721435




                  2,93721435























                      0














                      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);
                      }





                      share|improve this answer




























                        0














                        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);
                        }





                        share|improve this answer


























                          0












                          0








                          0







                          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);
                          }





                          share|improve this answer













                          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);
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '18 at 10:29









                          jdimkojdimko

                          608




                          608






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini