/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
namespace System.Web.Management{
public class SQLiteWebEventProvider : BufferedWebEventProvider
{
private DateTime _retryDate;
private string connectionString;
int _maxEventDetailsLength;
private int _commandTimeout;
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
connectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;
int.TryParse(config["maxEventDetailsLength"], out _maxEventDetailsLength);
if (this._maxEventDetailsLength == 0x7fffffff)
{
this._maxEventDetailsLength = -1;
}
int.TryParse(config["commandTimeout"], out _commandTimeout);
config.Remove("connectionStringName");
config.Remove("maxEventDetailsLength");
config.Remove("commandTimeout");
base.Initialize(name, config);
}
public override void ProcessEvent(WebBaseEvent eventRaised)
{
if (base.UseBuffering)
{
base.ProcessEvent(eventRaised);
}
else
{
this.WriteToSQL(new WebBaseEventCollection(new WebBaseEvent[] { eventRaised }), 0, new DateTime(0L));
}
}
public override void ProcessEventFlush(WebEventBufferFlushInfo flushInfo)
{
this.WriteToSQL(flushInfo.Events, flushInfo.EventsDiscardedSinceLastNotification, flushInfo.LastNotificationUtc);
}
private void PrepareParams(SQLiteCommand command)
{
command.Parameters.Add(new SQLiteParameter("$EventId", DbType.String, 0x20));
command.Parameters.Add(new SQLiteParameter("$EventTimeUtc", DbType.DateTime));
command.Parameters.Add(new SQLiteParameter("$EventTime", DbType.DateTime));
command.Parameters.Add(new SQLiteParameter("$EventType", DbType.String, 0x100));
command.Parameters.Add(new SQLiteParameter("$EventSequence", DbType.Decimal));
command.Parameters.Add(new SQLiteParameter("$EventOccurrence", DbType.Decimal));
command.Parameters.Add(new SQLiteParameter("$EventCode", DbType.Int32));
command.Parameters.Add(new SQLiteParameter("$EventDetailCode", DbType.Int32));
command.Parameters.Add(new SQLiteParameter("$Message", DbType.String, 0x400));
command.Parameters.Add(new SQLiteParameter("$ApplicationPath", DbType.String, 0x100));
command.Parameters.Add(new SQLiteParameter("$ApplicationVirtualPath", DbType.String, 0x100));
command.Parameters.Add(new SQLiteParameter("$MachineName", DbType.String, 0x100));
command.Parameters.Add(new SQLiteParameter("$RequestUrl", DbType.String, 0x400));
command.Parameters.Add(new SQLiteParameter("$ExceptionType", DbType.String, 0x100));
command.Parameters.Add(new SQLiteParameter("$Details", DbType.String));
}
private void FillParams(SQLiteCommand command, WebBaseEvent eventRaised)
{
Exception errorException = null;
WebRequestInformation requestInformation = null;
string str = null;
WebApplicationInformation applicationInformation = WebBaseEvent.ApplicationInformation;
int num = 0;
command.Parameters[num++].Value = eventRaised.EventID.ToString("N", CultureInfo.InstalledUICulture);
command.Parameters[num++].Value = eventRaised.EventTimeUtc;
command.Parameters[num++].Value = eventRaised.EventTime;
command.Parameters[num++].Value = eventRaised.GetType().ToString();
command.Parameters[num++].Value = eventRaised.EventSequence;
command.Parameters[num++].Value = eventRaised.EventOccurrence;
command.Parameters[num++].Value = eventRaised.EventCode;
command.Parameters[num++].Value = eventRaised.EventDetailCode;
command.Parameters[num++].Value = eventRaised.Message;
command.Parameters[num++].Value = applicationInformation.ApplicationPath;
command.Parameters[num++].Value = applicationInformation.ApplicationVirtualPath;
command.Parameters[num++].Value = applicationInformation.MachineName;
if (eventRaised is WebRequestEvent)
{
requestInformation = ((WebRequestEvent)eventRaised).RequestInformation;
}
else if (eventRaised is WebRequestErrorEvent)
{
requestInformation = ((WebRequestErrorEvent)eventRaised).RequestInformation;
}
else if (eventRaised is WebErrorEvent)
{
requestInformation = ((WebErrorEvent)eventRaised).RequestInformation;
}
else if (eventRaised is WebAuditEvent)
{
requestInformation = ((WebAuditEvent)eventRaised).RequestInformation;
}
command.Parameters[num++].Value = (requestInformation != null) ? requestInformation.RequestUrl : Convert.DBNull;
if (eventRaised is WebBaseErrorEvent)
{
errorException = ((WebBaseErrorEvent)eventRaised).ErrorException;
}
command.Parameters[num++].Value = (errorException != null) ? errorException.GetType().ToString() : Convert.DBNull;
str = eventRaised.ToString();
if ((this._maxEventDetailsLength != -1) && (str.Length > this._maxEventDetailsLength))
{
str = str.Substring(0, this._maxEventDetailsLength);
}
command.Parameters[num++].Value = str;
}
private void WriteToSQL(WebBaseEventCollection events, int eventsDiscardedByBuffer, DateTime lastNotificationUtc)
{
if (this._retryDate <= DateTime.UtcNow)
{
try
{
var connection = new SQLiteConnection(connectionString);
var command = new SQLiteCommand(@"insert into aspnet_WebEvent_Events(EventId,EventTimeUtc,EventTime,EventType,EventSequence,EventOccurrence,
EventCode,EventDetailCode,Message,ApplicationPath,ApplicationVirtualPath,
MachineName,RequestUrl,ExceptionType,Details)
values($EventId,$EventTimeUtc,$EventTime,$EventType,$EventSequence,$EventOccurrence,
$EventCode,$EventDetailCode,$Message,$ApplicationPath,$ApplicationVirtualPath,
$MachineName,$RequestUrl,$ExceptionType,$Details)", connection);
this.PrepareParams(command);
try
{
connection.Open();
foreach (WebBaseEvent event3 in events)
{
this.FillParams(command, event3);
command.ExecuteNonQuery();
}
}
finally
{
connection.Close();
}
try
{
//this.EventProcessingComplete(events);
}
catch
{
}
}
catch
{
double num = 30.0;
if (this._commandTimeout > -1)
{
num = this._commandTimeout;
}
this._retryDate = DateTime.UtcNow.AddSeconds(num);
throw;
}
}
}
}
}
|