private void AddWebRequestInformationDataFields(LogRecord logrec, WebRequestInformation reqinfo) { logrec.Identity = reqinfo.ThreadAccountName; logrec.AdditionalFields.Add("ClientIP", reqinfo.UserHostAddress); logrec.AdditionalFields.Add("Url", reqinfo.RequestUrl); if (reqinfo.Principal != null) { logrec.AdditionalFields.Add("LoggedUser", reqinfo.Principal.Identity.Name); } }
private void Add(WebBaseEvent p_eventRaised) { Exception errorException = null; WebRequestInformation requestInformation = null; if (p_eventRaised is WebRequestEvent) { requestInformation = ((WebRequestEvent)p_eventRaised).RequestInformation; } else if (p_eventRaised is WebRequestErrorEvent) { requestInformation = ((WebRequestErrorEvent)p_eventRaised).RequestInformation; } else if (p_eventRaised is WebErrorEvent) { requestInformation = ((WebErrorEvent)p_eventRaised).RequestInformation; } else if (p_eventRaised is WebAuditEvent) { requestInformation = ((WebAuditEvent)p_eventRaised).RequestInformation; } if (p_eventRaised is WebBaseErrorEvent) { errorException = ((WebBaseErrorEvent)p_eventRaised).ErrorException; } StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_Health("); strSql.Append("EventCode,Message,EventTime,RequestUrl,ExceptionType,ExceptionMessage)"); strSql.Append(" values ("); strSql.Append("@EventCode,@Message,@EventTime,@RequestUrl,@ExceptionType,@ExceptionMessage)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@EventCode", SqlDbType.Int, 4), new SqlParameter("@Message", SqlDbType.NVarChar, 50), new SqlParameter("@EventTime", SqlDbType.DateTime), new SqlParameter("@RequestUrl", SqlDbType.NVarChar, 255), new SqlParameter("@ExceptionType", SqlDbType.NVarChar, 255), new SqlParameter("@ExceptionMessage", SqlDbType.NVarChar, 255) }; parameters[0].Value = p_eventRaised.EventCode; parameters[1].Value = p_eventRaised.Message; parameters[2].Value = p_eventRaised.EventTime; parameters[3].Value = (requestInformation != null) ? requestInformation.RequestUrl : Convert.DBNull; parameters[4].Value = (errorException != null) ? errorException.GetType().ToString() : Convert.DBNull; parameters[5].Value = (errorException != null) ? errorException.Message : Convert.DBNull; object obj = SqlHelper.GetSingle(strSql.ToString(), parameters); }
/// <summary> /// Create a message source provider from the provided web request and optionally Uri /// </summary> /// <param name="requestInformation"></param> /// <param name="requestUri"></param> public WebRequestSourceProvider(WebRequestInformation requestInformation, Uri requestUri) { if (requestInformation == null) { return; } //by default try to use the logical path as the class & method. if ((requestUri != null) && (string.IsNullOrEmpty(requestUri.AbsolutePath) == false)) { string swappedPath = requestUri.AbsolutePath.Replace('.', '_'); //we can't handle inline periods because they'll cause a parsing problem. swappedPath = swappedPath.Replace('/', '.'); string reformatedPath = "Web Site" + (swappedPath.StartsWith(".") ? swappedPath : "." + swappedPath); ClassName = reformatedPath; } FileName = requestInformation.RequestPath; }
private static string GetRequestXml(WebRequestInformation requestInformation) { //make sure we actually have something. if (requestInformation == null) { return(null); } XmlDocument requestXml = new XmlDocument(); XmlElement requestNode = requestXml.CreateElement("requestInformation"); requestXml.AppendChild(requestNode); if (string.IsNullOrEmpty(requestInformation.RequestUrl) == false) { XmlElement requestUrlNode = requestXml.CreateElement("requestUrl"); requestUrlNode.InnerText = requestInformation.RequestUrl; requestNode.AppendChild(requestUrlNode); } if (string.IsNullOrEmpty(requestInformation.RequestPath) == false) { XmlElement requestPathNode = requestXml.CreateElement("requestPath"); requestPathNode.InnerText = requestInformation.RequestPath; requestNode.AppendChild(requestPathNode); } if (string.IsNullOrEmpty(requestInformation.UserHostAddress) == false) { XmlElement userHostAddressNode = requestXml.CreateElement("userHostAddress"); userHostAddressNode.InnerText = requestInformation.UserHostAddress; requestNode.AppendChild(userHostAddressNode); } if (string.IsNullOrEmpty(requestInformation.ThreadAccountName) == false) { XmlElement threadAccountNameNode = requestXml.CreateElement("threadAccountName"); threadAccountNameNode.InnerText = requestInformation.ThreadAccountName; requestNode.AppendChild(threadAccountNameNode); } return(requestXml.InnerXml); }
/// <summary> /// Processes the event passed to the provider. /// </summary> /// <param name="raisedEvent"></param> public override void ProcessEvent(WebBaseEvent raisedEvent) { //make SURE we're really running in the web server so we don't mess up aspnet_compiler. if (m_NotInAspNet) { return; } try { //lets see if this is an event we're going to ignore to keep the log clean. if (raisedEvent.EventCode == WebEventCodes.AuditUrlAuthorizationSuccess) { return; //this is an ignorable audit success, who cares. } if (raisedEvent.EventCode == WebEventCodes.AuditFileAuthorizationSuccess) { return; //this is an ignorable audit success, who cares. } if (raisedEvent.EventCode == WebEventCodes.ApplicationHeartbeat) { return; //the user has other ways of knowing we're alive, we don't want to record this. } if (raisedEvent.EventCode == WebEventCodes.ApplicationShutdown) { //call end session and tell it our sucess information Log.EndSession(SessionStatus.Normal, raisedEvent.Message); return; } string categoryName = "System.Events." + GetEventCodeCategoryName(raisedEvent.EventCode); string caption = raisedEvent.Message; string description = null; Exception exception = null; LogMessageSeverity severity; Uri requestUri = null; WebProcessInformation processInformation = null; WebRequestInformation requestInformation = null; WebThreadInformation threadInformation = null; //process types we explicitly understand first WebSuccessAuditEvent webSuccessAuditEvent; WebViewStateFailureAuditEvent webViewStateFailureAuditEvent; WebFailureAuditEvent webFailureAuditEvent; WebRequestEvent webRequestEvent; WebRequestErrorEvent webRequestErrorEvent; WebErrorEvent webErrorEvent; WebBaseErrorEvent webBaseErrorEvent; WebApplicationLifetimeEvent webApplicationLifetimeEvent; WebManagementEvent webManagementEvent; if ((webSuccessAuditEvent = raisedEvent as WebSuccessAuditEvent) != null) { //it's an otherwise unknown inheritor of the success event, treat it as such. severity = LogMessageSeverity.Verbose; processInformation = webSuccessAuditEvent.ProcessInformation; requestInformation = webSuccessAuditEvent.RequestInformation; } else if ((webViewStateFailureAuditEvent = raisedEvent as WebViewStateFailureAuditEvent) != null) { //it's an otherwise unknown inheritor of the failure audit event, treat it as such. severity = LogMessageSeverity.Error; exception = webViewStateFailureAuditEvent.ViewStateException; GenerateLogMessage(exception, ref caption, ref description); //override the generic caption we created earlier. processInformation = webViewStateFailureAuditEvent.ProcessInformation; requestInformation = webViewStateFailureAuditEvent.RequestInformation; } else if ((webFailureAuditEvent = raisedEvent as WebFailureAuditEvent) != null) { //it's an otherwise unknown inheritor of the failure event, treat it as such. severity = LogMessageSeverity.Warning; processInformation = webFailureAuditEvent.ProcessInformation; requestInformation = webFailureAuditEvent.RequestInformation; } else if ((webRequestEvent = raisedEvent as WebRequestEvent) != null) { //it's an otherwise unknown inheritor of the request event, treat it as such. severity = LogMessageSeverity.Information; processInformation = webRequestEvent.ProcessInformation; requestInformation = webRequestEvent.RequestInformation; } else if ((webRequestErrorEvent = raisedEvent as WebRequestErrorEvent) != null) { //it's an otherwise unknown inheritor of the request error event, treat it as such. severity = LogMessageSeverity.Error; exception = webRequestErrorEvent.ErrorException; GenerateLogMessage(exception, ref caption, ref description); //override the generic caption we created earlier. processInformation = webRequestErrorEvent.ProcessInformation; requestInformation = webRequestErrorEvent.RequestInformation; threadInformation = webRequestErrorEvent.ThreadInformation; } else if ((webErrorEvent = raisedEvent as WebErrorEvent) != null) { //it's an otherwise unknown inheritor of the error event, treat it as such. severity = LogMessageSeverity.Error; exception = webErrorEvent.ErrorException; GenerateLogMessage(exception, ref caption, ref description); //override the generic caption we created earlier. processInformation = webErrorEvent.ProcessInformation; requestInformation = webErrorEvent.RequestInformation; threadInformation = webErrorEvent.ThreadInformation; } else if ((webBaseErrorEvent = raisedEvent as WebBaseErrorEvent) != null) { //it's an otherwise unknown inheritor of the base error event, treat it as such. exception = webBaseErrorEvent.ErrorException; severity = LogMessageSeverity.Error; processInformation = webBaseErrorEvent.ProcessInformation; } else if ((webApplicationLifetimeEvent = raisedEvent as WebApplicationLifetimeEvent) != null) { //we use different severities for two scenarios: Compilation and startup/shutdown if ((raisedEvent.EventCode == WebEventCodes.ApplicationCompilationStart) || (raisedEvent.EventCode == WebEventCodes.ApplicationCompilationEnd)) { severity = LogMessageSeverity.Verbose; } else { severity = LogMessageSeverity.Information; } processInformation = webApplicationLifetimeEvent.ProcessInformation; } else if ((webManagementEvent = raisedEvent as WebManagementEvent) != null) { //it's an otherwise unknown inheritor of the management event, treat it as such. severity = LogMessageSeverity.Information; processInformation = webManagementEvent.ProcessInformation; } else { //overly simple initial implementation severity = LogMessageSeverity.Information; } //see if we can populate more fields based on ASP-NET objects string userName = null; if (processInformation != null) { userName = processInformation.AccountName; } if (threadInformation != null) { //thread user name better than process user name userName = threadInformation.ThreadAccountName; } string detailsXml = null; if (requestInformation != null) { //if there is a current principal, that's our favorite answer. IPrincipal currentPrincipal = requestInformation.Principal; IIdentity currentIdentity = null; if (currentPrincipal != null) { currentIdentity = currentPrincipal.Identity; } userName = currentIdentity != null ? currentIdentity.Name : requestInformation.ThreadAccountName; detailsXml = GetRequestXml(requestInformation); if (string.IsNullOrEmpty(requestInformation.RequestUrl) == false) { try { requestUri = new Uri(requestInformation.RequestUrl); } catch (Exception ex) { GC.KeepAlive(ex); #if DEBUG Log.RecordException(ex, "System", true); #endif } } } //three ways we create our message source provider: Exception (best), Thread info (gotta parse, but accurate), and Request INfo (fake location) IMessageSourceProvider requestSource = null; if (exception != null) { requestSource = new ExceptionSourceProvider(exception); } else if (requestInformation != null) { requestSource = new WebRequestSourceProvider(requestInformation, requestUri); } else if (raisedEvent.EventSource != null) { requestSource = new WebRequestSourceProvider(raisedEvent.EventSource); } //if we haven't figured out a class name by now, BS it using our event source. if ((requestSource as WebRequestSourceProvider != null) && (string.IsNullOrEmpty(requestSource.ClassName) && (raisedEvent.EventSource != null))) { (requestSource as WebRequestSourceProvider).SetSource(raisedEvent.EventSource); } Log.Write(severity, LogSystem, requestSource, userName, exception, LogWriteMode.Queued, detailsXml, categoryName, caption, description); } catch (Exception ex) { GC.KeepAlive(ex); #if DEBUG Log.RecordException(ex, "System", true); #endif } }
private void InitRequestInformation() { if (this._requestInfo == null) { this._requestInfo = new WebRequestInformation(); } }
private void InsertEvent(WebBaseEvent eventToInsert) { WebRequestInformation requestInfo = null; Exception exception = null; VAWebRequestInformation vaRequestInfo = null; DataRow row = _dataTable.NewRow(); row["EventID"] = eventToInsert.EventID; row["EventTimeUtc"] = eventToInsert.EventTimeUtc; row["EventTime"] = eventToInsert.EventTime; row["EventType"] = eventToInsert.GetType().ToString(); row["EventSequence"] = eventToInsert.EventSequence; row["EventOccurrence"] = eventToInsert.EventOccurrence; row["EventCode"] = eventToInsert.EventCode; row["EventDetailCode"] = eventToInsert.EventDetailCode; row["EventMessage"] = eventToInsert.Message; row["MachineName"] = WebBaseEvent.ApplicationInformation.MachineName; row["ApplicationPath"] = WebBaseEvent.ApplicationInformation.ApplicationPath; row["ApplicationVirtualPath"] = WebBaseEvent.ApplicationInformation.ApplicationVirtualPath; if (eventToInsert is WebRequestEvent) { requestInfo = ((WebRequestEvent)eventToInsert).RequestInformation; } else if (eventToInsert is WebRequestErrorEvent) { requestInfo = ((WebRequestErrorEvent)eventToInsert).RequestInformation; } else if (eventToInsert is WebErrorEvent) { requestInfo = ((WebErrorEvent)eventToInsert).RequestInformation; } else if (eventToInsert is WebAuditEvent) { requestInfo = ((WebAuditEvent)eventToInsert).RequestInformation; } if (eventToInsert is WebBaseErrorEvent) { exception = ((WebBaseErrorEvent)eventToInsert).ErrorException; } if (eventToInsert is VAWebRequestErrorEvent) { vaRequestInfo = ((VAWebRequestErrorEvent)eventToInsert).VARequestInformation; } else if (eventToInsert is VAWebRequestDocumentoDownloadEvent) { vaRequestInfo = ((VAWebRequestDocumentoDownloadEvent)eventToInsert).VARequestInformation; } if (requestInfo != null) { row["RequestUrl"] = requestInfo.RequestUrl; row["UserHostAddress"] = requestInfo.UserHostAddress; row["PrincipalIdentityIsAuthenticated"] = requestInfo.Principal.Identity.IsAuthenticated; if (requestInfo.Principal.Identity.IsAuthenticated) { row["PrincipalIdentityName"] = requestInfo.Principal.Identity.Name; } } if (exception != null) { row["ExceptionType"] = exception.GetType().ToString(); row["ExceptionMessage"] = exception.Message; row["Details"] = eventToInsert.ToString(true, true); row["WebEventTypeID"] = VAWebEventTypeEnum.Errore; } if (vaRequestInfo != null) { if (vaRequestInfo.UtenteID != null) { row["UtenteID"] = vaRequestInfo.UtenteID; row["UtenteNomeUtente"] = vaRequestInfo.NomeUtente; } if (vaRequestInfo.UrlReferrer != null) { row["RequestUrlReferrer"] = vaRequestInfo.UrlReferrer; } if (vaRequestInfo.UserAgent != null) { row["RequestUserAgent"] = vaRequestInfo.UserAgent; } if (vaRequestInfo.IntEntityID != null) { row["IntEntityID"] = vaRequestInfo.IntEntityID; } if (vaRequestInfo.GuidEntityID != null) { row["GuidEntityID"] = vaRequestInfo.GuidEntityID; } if (vaRequestInfo.EventTypeID != null) { row["WebEventTypeID"] = vaRequestInfo.EventTypeID; } } _dataTable.Rows.Add(row); }