maoyuan121 / elmah

Automatically exported from code.google.com/p/elmah
Apache License 2.0
0 stars 0 forks source link

Return ErrorId from Raise #148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What new or enhanced feature are you proposing?
For the ErrorSignal.FromCurrentContext().Raise() method to return the 
error id

What goal would this enhancement help you achieve?
I would like to give users the id for the error that was generated for 
correlation back to teh errors in the log. This would help us drive better 
communications between our end-users and server admins/developers. It 
would also be useful when logging to TFS so we can correlate between the 
error log and the TFS work item.

Original issue reported on code.google.com by SQFoun...@gmail.com on 7 Jan 2010 at 2:30

GoogleCodeExporter commented 9 years ago
So I guess you could return the error id in the events args of the Raised Event 
Args. For now there only seems to be the context and exception objects in there 
currently.

Original comment by SQFoun...@gmail.com on 7 Jan 2010 at 3:49

GoogleCodeExporter commented 9 years ago
Raising an error signal does not mean an error will be logged so an ID cannot 
be 
guaranteed or supplied through this mechanism. Raising an error signal simply 
informs listening parties of an error.

If you want to obtain the ID of a logged error, you need to subscribe to the 
Logged 
event of the ErrorLogModule. See the following post to get started:

http://groups.google.com/group/elmah/msg/fcedb430b121e2dc

The arguments of the logged event are represented by ErrorLoggedEventArgs:

http://code.google.com/p/elmah/source/browse/tags/REL-
1.1/src/Elmah/ErrorLogModule.cs#179

It contains a single Entry property whose Id property will give you what you're 
looking for. See also:

http://code.google.com/p/elmah/source/browse/tags/REL-
1.1/src/Elmah/ErrorLogEntry.cs#85

The ErrorLogModule module, when registered, subscribes to error signaling so 
when 
someone calls Raise, it will log the error.

I'm setting the status of this issue as WontFix as this functionality is 
already 
available.

If you need more help, please post to the discussion group instead:
http://groups.google.com/group/elmah

Original comment by azizatif on 7 Jan 2010 at 11:02

GoogleCodeExporter commented 9 years ago
The following patch adds this feature:

diff -r 2482ebd0a4ae src/Elmah/ErrorLogModule.cs
--- a/src/Elmah/ErrorLogModule.cs   Tue May 01 19:31:20 2012 +0200
+++ b/src/Elmah/ErrorLogModule.cs   Wed May 02 16:07:07 2012 -0700
@@ -84,15 +84,21 @@

         protected virtual void OnErrorSignaled(object sender, ErrorSignalEventArgs args)
         {
-            LogException(args.Exception, args.Context);
+            string id = LogException(args.Exception, args.Context);
+            if (!string.IsNullOrEmpty(id)) {
+                args.ErrorId = id;
+            }
         }

         /// <summary>
         /// Logs an exception and its context to the error log.
         /// </summary>
+        /// <returns>ErrorId, may be null since not all errors are 
logged</returns>

-        protected virtual void LogException(Exception e, HttpContextBase 
context)
+        protected virtual string LogException(Exception e, HttpContextBase 
context)
         {
+            string id = null;
+
             if (e == null)
                 throw new ArgumentNullException("e");

@@ -105,7 +111,7 @@
             OnFiltering(args);

             if (args.Dismissed)
-                return;
+                return id;

             //
             // Log away...
@@ -118,7 +124,7 @@
                 Error error = new Error(e, context);
                 ErrorLog log = GetErrorLog(context);
                 error.ApplicationName = log.ApplicationName;
-                string id = log.Log(error);
+                id = log.Log(error);
                 entry = new ErrorLogEntry(log, id, error);
             }
             catch (Exception localException)
@@ -137,6 +143,8 @@

             if (entry != null)
                 OnLogged(new ErrorLoggedEventArgs(entry));
+
+            return id;
         }

         /// <summary>
diff -r 2482ebd0a4ae src/Elmah/ErrorSignal.cs
--- a/src/Elmah/ErrorSignal.cs  Tue May 01 19:31:20 2012 +0200
+++ b/src/Elmah/ErrorSignal.cs  Wed May 02 16:07:07 2012 -0700
@@ -46,15 +46,19 @@
             Raise(e, null);
         }

-        public void Raise(Exception e, HttpContextBase context)
+        public string Raise(Exception e, HttpContextBase context)
         {
             if (context == null)
                 context = new HttpContextWrapper(HttpContext.Current);

             ErrorSignalEventHandler handler = Raised;

+            ErrorSignalEventArgs args = new ErrorSignalEventArgs(e, context);
+
             if (handler != null)
-                handler(this, new ErrorSignalEventArgs(e, context));
+                handler(this, args);
+
+            return args.ErrorId;
         }

         public static ErrorSignal FromCurrentContext()
@@ -154,6 +158,11 @@
             get { return _context; }
         }

+        /// <summary>
+        /// When you handle the signal, update this with the resulting ErrorId 
(if any)
+        /// </summary>
+        public string ErrorId { get; set; }
+
         public override string ToString()
         {
             return Mask.EmptyString(Exception.Message, Exception.GetType().FullName);
diff -r 2482ebd0a4ae src/Elmah/ErrorTweetModule.cs
--- a/src/Elmah/ErrorTweetModule.cs Tue May 01 19:31:20 2012 +0200
+++ b/src/Elmah/ErrorTweetModule.cs Wed May 02 16:07:07 2012 -0700
@@ -129,15 +129,20 @@

         protected virtual void OnErrorSignaled(object sender, ErrorSignalEventArgs args)
         {
-            LogException(args.Exception, args.Context);
+            string id = LogException(args.Exception, args.Context);
+            if (!string.IsNullOrEmpty(id)) {
+                args.ErrorId = id;
+            }        
         }

         /// <summary>
         /// Logs an exception and its context to the error log.
         /// </summary>

-        protected virtual void LogException(Exception e, HttpContextBase 
context)
+        protected virtual string LogException(Exception e, HttpContextBase 
context)
         {
+            string id = null;
+
             if (e == null)
                 throw new ArgumentNullException("e");

@@ -150,7 +155,7 @@
             OnFiltering(args);

             if (args.Dismissed)
-                return;
+                return id;

             //
             // Tweet away...
@@ -240,6 +245,8 @@

                 OnWebPostError(request, localException);
             }
+
+           return id;
         }

         private void OnWebPostError(WebRequest request, Exception e)

Original comment by robr...@robrich.org on 2 May 2012 at 11:09

GoogleCodeExporter commented 9 years ago
Hi. I can't apply diff's to files. 
Can you post patched files?
Thanks,

Original comment by flaviomi...@gmail.com on 3 Nov 2012 at 2:20