Created
January 14, 2012 20:12
-
-
Save JesseBuesking/1612723 to your computer and use it in GitHub Desktop.
Log4Net Glimpse plugin interfacing with MySql through Dapper and MiniProfiler's ProfiledDbConnection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web; | |
using Dapper; | |
using Glimpse.Core.Extensibility; | |
using MySite.WebGUI.Areas.Admin.Controllers; | |
using MvcMiniProfiler.Data; | |
using MySql.Data.MySqlClient; | |
using log4net; | |
using log4net.Appender; | |
using log4net.Repository.Hierarchy; | |
using log4net.Repository; | |
namespace MySite.WebGUI.GlimpsePlugins | |
{ | |
[GlimpsePlugin] | |
public class Log4NetPlugin : IGlimpsePlugin, IProvideGlimpseHelp, IProvideGlimpsePaging | |
{ | |
private string log4netConnectionString; | |
public Log4NetPlugin() | |
{ | |
ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["dbConnLog4Net"]; | |
if (ConnectionStringSettings == null || ConnectionStringSettings.ConnectionString.Trim() == "") | |
{ | |
throw new ConfigurationErrorsException("dbConnLog4Net connection string cannot be blank."); | |
} | |
log4netConnectionString = ConnectionStringSettings.ConnectionString; | |
} | |
public string Name | |
{ | |
get { return "Log4Net"; } | |
} | |
public string HelpUrl | |
{ | |
get { return "http://logging.apache.org/log4net/release/faq.html"; } | |
} | |
public int PageIndex | |
{ | |
get; | |
private set; | |
} | |
public int PageSize | |
{ | |
get { return 10; } | |
} | |
private readonly Guid _pagerKey = Guid.NewGuid(); | |
public Guid PagerKey | |
{ | |
get { return _pagerKey; } | |
} | |
public PagerType PagerType | |
{ | |
get { return PagerType.ContinuousPaging; } | |
} | |
public int TotalNumberOfRecords | |
{ | |
get; | |
private set; | |
} | |
public object GetData(HttpContextBase context) | |
{ | |
return GetData(context, 0); | |
} | |
public object GetData(HttpContextBase context, int pageIndex) | |
{ | |
return GetLogs(context, pageIndex, PageSize); | |
} | |
// TODO: Figure out how to stop Glimpse.axd from 403ing on IIS | |
// TODO: Figure out how to update the screen with more logs | |
public object GetLogs(HttpContextBase context, int pageIndex, int pageSize) | |
{ | |
// validate parameters | |
if (context == null) | |
return null; | |
using (ProfiledDbConnection connection = new ProfiledDbConnection(new MySqlConnection(log4netConnectionString), MvcMiniProfiler.MiniProfiler.Current)) | |
{ | |
connection.Open(); | |
int totalCount = Convert.ToInt32(connection.Query("SELECT COUNT(*) AS 'Count' FROM logs").FirstOrDefault().Count); | |
if (totalCount == 0) | |
return null; | |
// get logs from log4net | |
List<Log4NetModel> logs = connection.Query<Log4NetModel>("SELECT * " + | |
"FROM logs " + | |
"ORDER BY id DESC " + | |
"LIMIT @start, @end", new { start = pageIndex * pageSize, end = pageSize }).ToList(); | |
if (logs.Count == 0) | |
return null; | |
// create the header row | |
List<object[]> Data = new List<object[]> | |
{ | |
new object[] | |
{ | |
"Id", | |
"Date", | |
"Thread", | |
"Level", | |
"Logger", | |
"Message", | |
"Exception" | |
} | |
}; | |
foreach (Log4NetModel log in logs) | |
{ | |
string logtype = ""; | |
switch (log.level) | |
{ | |
case "ALL": | |
break; | |
case "DEBUG": | |
break; | |
case "INFO": | |
logtype = "info"; | |
break; | |
case "WARN": | |
logtype = "warn"; | |
break; | |
case "ERROR": | |
logtype = "error"; | |
break; | |
case "FATAL": | |
logtype = "fail"; | |
break; | |
} | |
Data.Add(new object[] { log.id, log.date, log.thread, log.level, log.logger, log.message, log.exception, logtype }); | |
} | |
// set the paging properties | |
PageIndex = pageIndex; | |
TotalNumberOfRecords = totalCount; | |
// return the log page | |
return Data; | |
} | |
} | |
public void SetupInit() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment