event EventHandler<PageSendEventArgs> OnResourceSend
Event Data
The event handler receives an argument of type PageSendEventArgs containing data related to this event. The following PageSendEventArgs properties provide information specific to this event.
Property | Description |
---|---|
Request | The page request generated after all filter and middleware had the ability to process it |
Remarks
This is a non blocking request that occurs after the page was send to the HTTPResponse pipeline.
This event is triggered after logging and reporting activity has concluded.
Example
The included example shows 1 use case where a page is send to the developers if that page has had exceptions during it's life.
You can always add a exception to a IPageRequest by dependency injection in your code
public class MyFireWall:FireWallBase { //avoid sending more than 1 email per url during the life of the web application private readonly HashSet<Uri> _reported = new HashSet<Uri>(); ILogger<MyFireWall> _logger; public MyFireWall(ILoggerFactory factory, IMemoryCache cache, ILatLongRepository geo) : base(loggerFactory: factory, memoryCache: cache, latLongRepository:geo) { base.Trigger_OnFireWallCreated(this); _logger = factory.CreateLogger<MyFireWall>(); OnResourceSend += MyFireWall_OnResourceSend; } private void MyFireWall_OnResourceSend(object sender, PageSendEventArgs e) { // if exception occurred and the url was not reported in this session if (e.Request.Exception is not null && _reported.Add(e.Request.OriginalUrl)) { var sb = new StringBuilder(); sb.AppendLine($"Page {e.Request.OriginalUrl} from {e.Request.Country} caused {e.Request.Exceptions.Count} exception(s)"); foreach (var item in e.Request.Exceptions) { sb.AppendLine($" {item.GetType().Name} exception Message: {item.Message}"); sb.AppendLine($" {item.ClassName()}.{item.MethodName()} in {item.FileName()}:{item.CodeLineNumber()}"); sb.AppendLine(item.StackTrace); sb.AppendLine(); } //helper class to send emails to developers, use your email implementation here MailClient.SendDeveloperError(body: sb.ToString(),e.Request.ToString()); } } protected override void Dispose(bool disposing) { if (_reported.Count > 0) { _logger?.Lazy().LogWarning("The following {count} errors where recorded on these urls \n\t {urls}",_reported.Count, string.Join("\n\t", _reported.Select(s => s.PathAndQuery))); } base.Dispose(disposing); } } internal class MailClient { internal static void SendDeveloperError(string body, string subject) { try { using MailMessage mail = new MailMessage(); using SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your_email_address@gmail.com"); mail.To.Add("to_address"); mail.Subject = subject; mail.Body = body; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); } catch { } } }
Requirements
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
See Also