#region Copyright
// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
//
// This file is part of Diagnostic Explorer.
//
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Diagnostic Explorer 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer. If not, see <http://www.gnu.org/licenses/>.
//
// http://diagexplorer.sourceforge.net/
#endregion
using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
namespace DiagnosticExplorer
{
public class SqlLoggingService : ILogReader, ILogWriter
{
public void WriteMessage(DiagnosticMsg msg)
{
Database db = DatabaseFactory.CreateDatabase();
using (DbCommand cmd = db.GetStoredProcCommand("Diagnostics_Insert"))
{
db.AddInParameter(cmd, "Date", DbType.DateTime, msg.Date);
db.AddInParameter(cmd, "Category", DbType.String, msg.Category);
db.AddInParameter(cmd, "Severity", DbType.Int32, msg.Level);
db.AddInParameter(cmd, "Process", DbType.String, msg.Process);
db.AddInParameter(cmd, "User", DbType.String, msg.User);
db.AddInParameter(cmd, "Machine", DbType.String, msg.Machine);
db.AddInParameter(cmd, "Message", DbType.String, msg.Message);
db.ExecuteNonQuery(cmd);
}
}
public IList<DiagnosticMsg> GetMessages(LogQuery query)
{
Database db = DatabaseFactory.CreateDatabase();
using (DbCommand cmd = db.GetStoredProcCommand("Diagnostics_Get"))
{
db.AddInParameter(cmd, "StartDate", DbType.DateTime, query.StartDate);
db.AddInParameter(cmd, "EndDate", DbType.DateTime, query.EndDate);
db.AddInParameter(cmd, "Machine", DbType.String, query.Machine);
db.AddInParameter(cmd, "Message", DbType.String, query.Message);
db.AddInParameter(cmd, "MinSeverity", DbType.Int32, query.MinSeverity);
db.AddInParameter(cmd, "Process", DbType.String, query.Process);
db.AddInParameter(cmd, "MaxRecords", DbType.Int32, query.MaxRecords);
List<DiagnosticMsg> messages = new List<DiagnosticMsg>();
using (IDataReader reader = db.ExecuteReader(cmd))
{
while (reader.Read())
{
DiagnosticMsg msg = new DiagnosticMsg();
msg.Date = reader.GetDateTime("Date").Value;
msg.Level = reader.GetInt32("Severity").Value;
msg.Category = reader.GetString("Category");
msg.Process = reader.GetString("Process");
msg.User = reader.GetString("User");
msg.Machine = reader.GetString("Machine");
msg.Message = reader.GetString("Message");
messages.Add(msg);
}
}
return messages;
}
}
}
public static class DataReaderExtensions
{
public static string GetString(this IDataReader reader, string field)
{
int ordinal = reader.GetOrdinal(field);
if (reader.IsDBNull(ordinal))
return null;
return reader.GetString(ordinal);
}
public static DateTime? GetDateTime(this IDataReader reader, string field)
{
int ordinal = reader.GetOrdinal(field);
if (reader.IsDBNull(ordinal))
return null;
return reader.GetDateTime(ordinal);
}
public static int? GetInt32(this IDataReader reader, string field)
{
int ordinal = reader.GetOrdinal(field);
if (reader.IsDBNull(ordinal))
return null;
return reader.GetInt32(ordinal);
}
}
}