⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlwebeventprovider.cs

📁 MasterPage(母版页) 母版页(MasterPage)就相当于模板页
💻 CS
📖 第 1 页 / 共 2 页
字号:
                details = details.Substring(0, _maxEventDetailsLength);
            }
            sqlCommand.Parameters[n++].Value = details;
        }

        // Remove CAS from sample: [PermissionSet(SecurityAction.InheritanceDemand, Unrestricted = true)]
        // Remove CAS from sample: [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        // Remove CAS from sample: [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        void WriteToSQL(ICollection events, int eventsDiscardedByBuffer, DateTime lastNotificationUtc)
        {
            // We don't want to send any more events until we've waited until the _retryDate (which defaults to minValue)
            if (_retryDate > DateTime.UtcNow)
            {
                return;
            }

            try
            {
                SqlConnectionHolder sqlConnHolder = SqlConnectionHelper.GetConnection(_sqlConnectionString, true);

                SqlCommand sqlCommand = new SqlCommand(SP_LOG_EVENT);

                CheckSchemaVersion(sqlConnHolder.Connection);

                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Connection = sqlConnHolder.Connection;

                if (_commandTimeout > -1)
                {
                    sqlCommand.CommandTimeout = _commandTimeout;
                }

                PrepareParams(sqlCommand);

                try
                {
                    sqlConnHolder.Open(null, true);
                    Interlocked.Increment(ref _connectionCount);

                    if (eventsDiscardedByBuffer != 0)
                    {
                        WebBaseEvent infoEvent = new MyWebBaseEvent(
                            SR.GetString(SR.Sql_webevent_provider_events_dropped,
                                eventsDiscardedByBuffer.ToString(CultureInfo.InstalledUICulture),
                                lastNotificationUtc.ToString("r", CultureInfo.InstalledUICulture)),
                                null,
                                WebEventCodes.WebEventProviderInformation,
                                WebEventCodes.SqlProviderEventsDropped);

                        FillParams(sqlCommand, infoEvent);
                        sqlCommand.ExecuteNonQuery();
                    }

                    foreach (WebBaseEvent eventRaised in events)
                    {
                        FillParams(sqlCommand, eventRaised);
                        sqlCommand.ExecuteNonQuery();
                    }
                }
                finally
                {
                    sqlConnHolder.Close();
                    Interlocked.Decrement(ref _connectionCount);
                }

                try
                {
                    EventProcessingComplete(events);
                }
                catch
                {
                    // Ignore all errors.
                }
            }
            catch
            {
                // For any failure, we will wait at least 30 seconds or _commandTimeout before trying again
                double timeout = 30;
                if (_commandTimeout > -1)
                {
                    timeout = (double)_commandTimeout;
                }
                _retryDate = DateTime.UtcNow.AddSeconds(timeout);
                throw;
            }
        }

        public override void ProcessEvent(WebBaseEvent eventRaised)
        {
            if (UseBuffering)
            {
                base.ProcessEvent(eventRaised);
            }
            else
            {
                // Remove Debug.Trace from sample Debug.Trace("SqlWebEventProvider", "Writing event to SQL: event=" + eventRaised.GetType().Name);
                WriteToSQL(new MyWebBaseEventCollection(eventRaised), 0, new DateTime(0));
            }
        }

        protected virtual void EventProcessingComplete(ICollection raisedEvents)
        {
        }

        public override void Shutdown()
        {
            try
            {
                Flush();
            }
            finally
            {
                base.Shutdown();
            }

            // Need to wait until all connections are gone before returning here
            // Sleep for 2x the command timeout in 1 sec intervals then give up, default timeout is 30 sec
            if (_connectionCount > 0)
            {
                int sleepAttempts = _commandTimeout * 2;
                if (sleepAttempts <= 0)
                {
                    sleepAttempts = 60;
                }
                // Check every second
                while (_connectionCount > 0 && sleepAttempts > 0)
                {
                    --sleepAttempts;
                    Thread.Sleep(1000);
                }
            }
        }
    }
    public class MyWebBaseEvent : WebBaseEvent
    {
        public MyWebBaseEvent(string message, Object eventSource, int eventCode, int eventDetailCode)
            : base(message, eventSource, eventCode, eventDetailCode)
        { }
    }
    public sealed class MyWebBaseEventCollection : ReadOnlyCollectionBase
    {
        public MyWebBaseEventCollection(ICollection events)
        {
            if (events == null)
            {
                throw new ArgumentNullException("events");
            }

            foreach (WebBaseEvent eventRaised in events)
            {
                InnerList.Add(eventRaised);
            }
        }

        internal MyWebBaseEventCollection(WebBaseEvent eventRaised)
        {
            if (eventRaised == null)
            {
                throw new ArgumentNullException("eventRaised");
            }

            InnerList.Add(eventRaised);
        }

        // overloaded collection access methods 
        public WebBaseEvent this[int index]
        {
            get
            {
                return (WebBaseEvent)InnerList[index];
            }
        }

        public int IndexOf(WebBaseEvent value)
        {
            return InnerList.IndexOf(value);
        }

        public bool Contains(WebBaseEvent value)
        {
            return InnerList.Contains(value);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -