Example #1
0
        /// <inheritdoc/>
        public override void AddLog(LogInfo logInfo)
        {
            string configPortalID = logInfo.LogPortalID != Null.NullInteger
                                        ? logInfo.LogPortalID.ToString()
                                        : "*";
            var logTypeConfigInfo = this.GetLogTypeConfigInfoByKey(logInfo.LogTypeKey, configPortalID);

            if (logTypeConfigInfo == null || logTypeConfigInfo.LoggingIsActive == false)
            {
                return;
            }

            logInfo.LogConfigID = logTypeConfigInfo.ID;
            var logQueueItem = new LogQueueItem {
                LogInfo = logInfo, LogTypeConfigInfo = logTypeConfigInfo
            };
            SchedulingProvider scheduler = SchedulingProvider.Instance();

            if (scheduler == null || logInfo.BypassBuffering || SchedulingProvider.Enabled == false ||
                scheduler.GetScheduleStatus() == ScheduleStatus.STOPPED || !Host.EventLogBuffer)
            {
                WriteLog(logQueueItem);
            }
            else
            {
                LogQueue.Add(logQueueItem);
            }
        }
Example #2
0
        /// <inheritdoc/>
        public override void PurgeLogBuffer()
        {
            if (!LockQueueLog.TryEnterWriteLock(WriterLockTimeout))
            {
                return;
            }

            try
            {
                for (int i = LogQueue.Count - 1; i >= 0; i += -1)
                {
                    LogQueueItem logQueueItem = LogQueue[i];

                    // in case the log was removed
                    // by another thread simultaneously
                    if (logQueueItem != null)
                    {
                        WriteLog(logQueueItem);
                        LogQueue.Remove(logQueueItem);
                    }
                }
            }
            finally
            {
                LockQueueLog.ExitWriteLock();
            }

            DataProvider.Instance().PurgeLog();
        }
Example #3
0
        private static void WriteLog(LogQueueItem logQueueItem)
        {
            LogTypeConfigInfo logTypeConfigInfo = null;

            try
            {
                logTypeConfigInfo = logQueueItem.LogTypeConfigInfo;
                if (logTypeConfigInfo != null)
                {
                    LogInfo objLogInfo    = logQueueItem.LogInfo;
                    string  logProperties = objLogInfo.LogProperties.Serialize();
                    DataProvider.Instance().AddLog(
                        objLogInfo.LogGUID,
                        objLogInfo.LogTypeKey,
                        objLogInfo.LogUserID,
                        objLogInfo.LogUserName,
                        objLogInfo.LogPortalID,
                        objLogInfo.LogPortalName,
                        objLogInfo.LogCreateDate,
                        objLogInfo.LogServerName,
                        logProperties,
                        Convert.ToInt32(objLogInfo.LogConfigID),
                        objLogInfo.Exception,
                        logTypeConfigInfo.EmailNotificationIsActive);
                    if (logTypeConfigInfo.EmailNotificationIsActive)
                    {
                        if (LockNotif.TryEnterWriteLock(ReaderLockTimeout))
                        {
                            try
                            {
                                if (logTypeConfigInfo.NotificationThreshold == 0)
                                {
                                    string str = logQueueItem.LogInfo.Serialize();
                                    Mail.Mail.SendEmail(logTypeConfigInfo.MailFromAddress, logTypeConfigInfo.MailToAddress, "Event Notification", string.Format("<pre>{0}</pre>", HttpUtility.HtmlEncode(str)));
                                }
                            }
                            finally
                            {
                                LockNotif.ExitWriteLock();
                            }
                        }
                    }
                }
            }
            catch (SqlException exc)
            {
                Logger.Error(exc);
                WriteError(logTypeConfigInfo, exc, "SQL Exception", SqlUtils.TranslateSQLException(exc));
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                WriteError(logTypeConfigInfo, exc, "Unhandled Error", exc.Message);
            }
        }
        //--------------------------------------------------------------
        //Method to add a log entry
        //--------------------------------------------------------------
        public override void AddLog( LogInfo objLogInfo )
        {
            string ConfigPortalID;
            if( objLogInfo.LogPortalID != Null.NullInteger )
            {
                ConfigPortalID = objLogInfo.LogPortalID.ToString();
            }
            else
            {
                ConfigPortalID = "*";
            }

            LogTypeConfigInfo objLogTypeConfigInfo;
            objLogTypeConfigInfo = GetLogTypeConfig( ConfigPortalID, objLogInfo.LogTypeKey );
            if (objLogTypeConfigInfo != null && objLogTypeConfigInfo.LoggingIsActive)
            {
                objLogInfo.LogFileID = objLogTypeConfigInfo.ID;
                objLogInfo.LogCreateDateNum = DateToNum(objLogInfo.LogCreateDate);
                LogQueueItem objLogQueueItem = new LogQueueItem();
                objLogQueueItem.LogString = objLogInfo.Serialize();
                objLogQueueItem.LogTypeConfigInfo = objLogTypeConfigInfo;

                bool UseEventLogBuffer = true;
                if (Globals.HostSettings.ContainsKey("EventLogBuffer"))
                {
                    if (Convert.ToString(Globals.HostSettings["EventLogBuffer"]) != "Y")
                    {
                        UseEventLogBuffer = false;
                    }
                }
                else
                {
                    UseEventLogBuffer = false;
                }

                SchedulingProvider scheduler = SchedulingProvider.Instance();
                if (objLogInfo.BypassBuffering | SchedulingProvider.Enabled == false | scheduler.GetScheduleStatus()  == ScheduleStatus.STOPPED | UseEventLogBuffer == false)
                {
                    WriteLog(objLogQueueItem);
                }
                else
                {
                    LogQueue.Add(objLogQueueItem);
                }
            }
        }
        private void WriteLog( LogQueueItem objLogQueueItem )
        {
            //--------------------------------------------------------------
            //Write the log entry
            //--------------------------------------------------------------
            FileStream fs = null;
            StreamWriter sw = null;

            LogTypeConfigInfo objLogTypeConfigInfo = objLogQueueItem.LogTypeConfigInfo;
            string LogString = objLogQueueItem.LogString;

            try
            {
                if( !String.IsNullOrEmpty(objLogTypeConfigInfo.LogFileNameWithPath) )
                {
                    //--------------------------------------------------------------
                    // Write the entry to the log.
                    //--------------------------------------------------------------
                    lockLog.AcquireWriterLock( WriterLockTimeout );
                    int intAttempts = 0;
                    //wait for up to 100 milliseconds for the file
                    //to be unlocked if it is not available
                    while( !( fs != null || intAttempts == 100 ) )
                    {
                        intAttempts++;
                        try
                        {
                            fs = new FileStream( objLogTypeConfigInfo.LogFileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None );
                        }
                        catch( IOException )
                        {
                            Thread.Sleep( 1 );
                        }
                    }

                    if( fs == null )
                    {
                        if( HttpContext.Current != null )
                        {
                            HttpContext.Current.Response.Write( "An error has occurred writing to the exception log." );
                            HttpContext.Current.Response.End();
                        }
                    }
                    else
                    {
                        //--------------------------------------------------------------
                        //Instantiate a new StreamWriter
                        //--------------------------------------------------------------
                        sw = new StreamWriter( fs, Encoding.UTF8 );
                        long FileLength;
                        FileLength = fs.Length;
                        //--------------------------------------------------------------
                        //check to see if this file is new
                        //--------------------------------------------------------------
                        if( FileLength > 0 )
                        {
                            //--------------------------------------------------------------
                            //file is not new, set the position to just before
                            //the closing root element tag
                            //--------------------------------------------------------------
                            fs.Position = FileLength - 9;
                        }
                        else
                        {
                            //--------------------------------------------------------------
                            //file is new, create the opening root element tag
                            //--------------------------------------------------------------
                            LogString = "<logs>" + LogString;
                        }

                        //--------------------------------------------------------------
                        //write out our exception
                        //--------------------------------------------------------------
                        sw.WriteLine( LogString + "</logs>" );
                        sw.Flush();
                    }
                    if( sw != null )
                    {
                        sw.Close();
                    }
                    if( fs != null )
                    {
                        fs.Close();
                    }
                }
                if( objLogTypeConfigInfo.EmailNotificationIsActive == true )
                {
                    try
                    {
                        lockNotif.AcquireWriterLock( ReaderLockTimeout );

                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml( LogString );

                        //If the threshold for email notifications is
                        //set to 0, send an email notification each
                        //time a log entry is written.

                        if( objLogTypeConfigInfo.NotificationThreshold == 0 )
                        {
                            Mail.Mail.SendMail( objLogTypeConfigInfo.MailFromAddress, objLogTypeConfigInfo.MailToAddress, "", "Event Notification", xmlDoc.InnerXml, "", "", "", "", "", "" );
                        }
                        else if( objLogTypeConfigInfo.LogTypeKey != "LOG_NOTIFICATION_FAILURE" )
                        {
                            XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
                            try
                            {
                                xmlPendingNotificationsDoc.Load( GetFilePath( PendingNotificationsFile ) );
                            }
                            catch( FileNotFoundException )
                            {
                                //file not created yet
                                xmlPendingNotificationsDoc.LoadXml( "<PendingNotifications></PendingNotifications>" );
                            }
                            XmlNode xmlLogNode;
                            xmlLogNode = xmlPendingNotificationsDoc.ImportNode( xmlDoc.FirstChild, true );

                            XmlAttribute xmlAttrib;
                            xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute( "MailFromAddress" );
                            xmlAttrib.Value = Convert.ToString( objLogTypeConfigInfo.MailFromAddress );
                            xmlLogNode.Attributes.Append( xmlAttrib );

                            xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute( "NotificationLogTypeKey" );
                            xmlAttrib.Value = Convert.ToString( objLogTypeConfigInfo.LogTypeKey );
                            xmlLogNode.Attributes.Append( xmlAttrib );

                            xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute( "LogTypePortalID" );
                            if( objLogTypeConfigInfo.LogTypePortalID == "-1" )
                            {
                                xmlAttrib.Value = "*";
                            }
                            else
                            {
                                xmlAttrib.Value = objLogTypeConfigInfo.LogTypePortalID;
                            }
                            xmlLogNode.Attributes.Append( xmlAttrib );

                            XmlElement x;
                            x = xmlPendingNotificationsDoc.CreateElement( "EmailAddress" );
                            x.InnerText = Convert.ToString( objLogTypeConfigInfo.MailToAddress );
                            xmlLogNode.AppendChild( x );

                            xmlPendingNotificationsDoc.DocumentElement.AppendChild( xmlLogNode );
                            xmlPendingNotificationsDoc.Save( GetFilePath( PendingNotificationsFile ) );
                        }
                    }
                    finally
                    {
                        lockNotif.ReleaseWriterLock();
                    }
                }
                //--------------------------------------------------------------
                //handle the more common exceptions up
                //front, leave less common ones to the end
                //--------------------------------------------------------------
            }
            catch( UnauthorizedAccessException exc )
            {
                if( HttpContext.Current != null )
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader( response, "Unauthorized Access Error" );

                    string strMessage = exc.Message + " The Windows User Account listed below must have Read/Write Privileges to this path.";
                    HtmlUtils.WriteError( response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage );

                    HtmlUtils.WriteFooter( response );
                    response.End();
                }
            }
            catch( DirectoryNotFoundException exc )
            {
                if( HttpContext.Current != null )
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader( response, "Directory Not Found Error" );

                    string strMessage = exc.Message;
                    HtmlUtils.WriteError( response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage );

                    HtmlUtils.WriteFooter( response );
                    response.End();
                }
            }
            catch( PathTooLongException exc )
            {
                if( HttpContext.Current != null )
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader( response, "Path Too Long Error" );

                    string strMessage = exc.Message;
                    HtmlUtils.WriteError( response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage );

                    HtmlUtils.WriteFooter( response );
                    response.End();
                }
            }
            catch( IOException exc )
            {
                if( HttpContext.Current != null )
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader( response, "IO Error" );

                    string strMessage = exc.Message;
                    HtmlUtils.WriteError( response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage );

                    HtmlUtils.WriteFooter( response );
                    response.End();
                }
            }
            catch( Exception exc )
            {
                if( HttpContext.Current != null )
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader( response, "Unhandled Error" );

                    string strMessage = exc.Message;
                    HtmlUtils.WriteError( response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage );

                    HtmlUtils.WriteFooter( response );
                    response.End();
                }
            }
            finally
            {
                if( sw != null )
                {
                    sw.Close();
                }
                if( fs != null )
                {
                    fs.Close();
                }

                lockLog.ReleaseWriterLock();
            }
        }
Example #6
0
 public override void AddLog(LogInfo logInfo)
 {
     string configPortalID = logInfo.LogPortalID != Null.NullInteger 
                                 ? logInfo.LogPortalID.ToString() 
                                 : "*";
     var logTypeConfigInfo = GetLogTypeConfigInfoByKey(logInfo.LogTypeKey, configPortalID);
     if (logTypeConfigInfo == null || logTypeConfigInfo.LoggingIsActive == false)
     {
         return;
     }
     logInfo.LogConfigID = logTypeConfigInfo.ID;
     var logQueueItem = new LogQueueItem {LogInfo = logInfo, LogTypeConfigInfo = logTypeConfigInfo};
     SchedulingProvider scheduler = SchedulingProvider.Instance();
     if (scheduler == null || logInfo.BypassBuffering || SchedulingProvider.Enabled == false 
         || scheduler.GetScheduleStatus() == ScheduleStatus.STOPPED || !Host.EventLogBuffer)
     {
         WriteLog(logQueueItem);
     }
     else
     {
         LogQueue.Add(logQueueItem);
     }
 }
Example #7
0
     private static void WriteLog(LogQueueItem logQueueItem)
     {
         LogTypeConfigInfo logTypeConfigInfo = null;
         try
         {
             logTypeConfigInfo = logQueueItem.LogTypeConfigInfo;
             if (logTypeConfigInfo != null)
             {
                 LogInfo objLogInfo = logQueueItem.LogInfo;
                 string logProperties = objLogInfo.LogProperties.Serialize();
                 DataProvider.Instance().AddLog(objLogInfo.LogGUID,
                                                objLogInfo.LogTypeKey,
                                                objLogInfo.LogUserID,
                                                objLogInfo.LogUserName,
                                                objLogInfo.LogPortalID,
                                                objLogInfo.LogPortalName,
                                                objLogInfo.LogCreateDate,
                                                objLogInfo.LogServerName,
                                                logProperties,
                                                Convert.ToInt32(objLogInfo.LogConfigID));
                 if (logTypeConfigInfo.EmailNotificationIsActive)
                 {
                     LockNotif.AcquireWriterLock(ReaderLockTimeout);
                     try
                     {
                         if (logTypeConfigInfo.NotificationThreshold == 0)
                         {
                             string str = logQueueItem.LogInfo.Serialize();
 
                             Mail.Mail.SendEmail(logTypeConfigInfo.MailFromAddress, logTypeConfigInfo.MailToAddress, "Event Notification", str);
                         }
                     }
                     finally
                     {
                         LockNotif.ReleaseWriterLock();
                     }
                 }
             }
         }
         catch (SqlException exc)
         {
             Logger.Error(exc);
             WriteError(logTypeConfigInfo, exc, "SQL Exception", SqlUtils.TranslateSQLException(exc));
         }
         catch (Exception exc)
         {
             Logger.Error(exc);
             WriteError(logTypeConfigInfo, exc, "Unhandled Error", exc.Message);
         }
     }