protected abstract void SendEvent (Event e);
protected override void SendEvent (Event e) { // Determine file where to persist the error: string path = null; if (errorsCachePath != null) { var file = String.Format ("{0}.json", DateTime.UtcNow.ToBinary ()); path = Path.Combine (errorsCachePath, file); } Stream eventStream = null; Stream notifStream = null; try { // Serialize the event: eventStream = TryStoreEvent (e, path); if (storeOnly) { storeOnly = false; return; } // Combine into a valid payload: notifStream = MakeNotification (new Stream[] { eventStream }); SendNotification (notifStream).ContinueWith ((t) => { try { if (t.Result) { // On successful response delete the stored file: try { File.Delete (path); } catch (Exception ex) { LogError (String.Format ("Failed to clean up stored event: {0}", ex)); } } } finally { if (notifStream != null) { // Also disposes of the eventStream notifStream.Dispose (); } } }); } catch (Exception ex) { // Something went wrong... LogError (String.Format ("Failed to send notification: {0}", ex)); if (notifStream != null) { // Also disposes of the eventStream notifStream.Dispose (); } else if (eventStream != null) { eventStream.Dispose (); } } }
public void Notify (Exception e, ErrorSeverity severity = ErrorSeverity.Error, Metadata extraMetadata = null) { if (!ShouldNotify) return; if (IgnoredExceptions != null && IgnoredExceptions.Contains (e.GetType ())) return; var md = new Metadata (metadata); if (extraMetadata != null) { md.Merge (extraMetadata); } var ev = new Event () { User = userInfo, App = GetAppInfo (), AppState = GetAppState (), System = GetSystemInfo (), SystemState = GetSystemState (), Context = Context, Severity = severity, Exceptions = ConvertExceptionTree (e), Metadata = md, }; SendEvent (ev); }
private Stream TryStoreEvent (Event e, string path) { var json = new MemoryStream ( System.Text.Encoding.UTF8.GetBytes ( JsonConvert.SerializeObject (e))); // Don't even try storing to disk when invalid path if (path == null) return json; FileStream output = null; try { output = new FileStream (path, FileMode.CreateNew); json.CopyTo (output); output.Flush (); output.Seek (0, SeekOrigin.Begin); json.Dispose (); return output; } catch (IOException ex) { LogError (String.Format ("Failed to store error to disk: {0}", ex)); // Failed to store to disk (full?), return json memory stream instead if (output != null) { output.Dispose (); } json.Seek (0, SeekOrigin.Begin); return json; } }