The Sitecore log analyser is a great tool for viewing patterns in log files over time.
If getting access to log files is slow or you simply want a quick summary of certain log files then hopefully the following might help. You can select the date range, filter terms and then view all log entries which fulfil that pattern.
I’d consider it to live in the same realm as the ‘/sitecore/admin’ pages as it can reveal critical site information (hence the admin check).
The front end isn’t the most glamourous but serves its purpose:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LogViewer.aspx.cs" Inherits="###.Custom.LogViewer" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div id="LogViewerPanel" runat="server"> Filter (| separate terms): <asp:TextBox runat="server" ID="FilterTextBox" Width="250"></asp:TextBox> Start Date (yyyy/MM/dd): <asp:TextBox runat="server" ID="StartDateTextBox"></asp:TextBox> End Date (yyyy/MM/dd): <asp:TextBox runat="server" ID="EndDateTextBox"></asp:TextBox> <asp:Button runat="server" Text="Go" OnClick="SearchClick"/> <hr /> <asp:TextBox runat="server" ID="ResultTextBox" TextMode="MultiLine" Width="1200" Height="600"></asp:TextBox> </div> </form> </body> </html> |
and then the code behind:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Sitecore.Configuration; using Sitecore.IO; namespace ###.Custom { public partial class LogViewer : System.Web.UI.Page { const string DateFormat = "yyyy/MM/dd"; protected void Page_Load(object sender, EventArgs e) { LogViewerPanel.Visible = Sitecore.Context.User.IsAdministrator; ResultTextBox.Text = ""; if (!IsPostBack) { StartDateTextBox.Text = EndDateTextBox.Text = DateTime.Now.ToString(DateFormat); } } protected void SearchClick(object sender, EventArgs e) { DateTime startDate = DateTime.ParseExact(StartDateTextBox.Text, DateFormat, CultureInfo.InvariantCulture); DateTime endDate = DateTime.ParseExact(EndDateTextBox.Text, DateFormat, CultureInfo.InvariantCulture); if (endDate < startDate) { Dump("End date must be after start date"); return; } if ((endDate - startDate).Days > 7) { Dump("You can only pick a range of 7 days"); return; } FindLogFiles(startDate, endDate, FilterTextBox.Text); } private void FindLogFiles(DateTime startDate, DateTime endDate, string filter) { Dump("Reading logs for: ", startDate.ToShortDateString(), " to: ", endDate.ToShortDateString(), " with filter: ", filter); List<DateTime> dates = new List<DateTime>(); dates.Add(startDate); while (startDate < endDate) { startDate = startDate.AddDays(1); dates.Add(startDate); } string logFolder = FileUtil.MapPath(Settings.LogFolder); DirectoryInfo di = new DirectoryInfo(logFolder); string logFilePrefix = "log"; List<FileInfo> validLogFiles = new List<FileInfo>(); foreach (FileInfo fi in di.GetFiles(logFilePrefix+"*")) { if (fi.Name.ToLower().StartsWith(logFilePrefix)) { foreach (DateTime dateTime in dates) { if (fi.Name.StartsWith(logFilePrefix + "." + dateTime.ToString("yyyyMMdd"))) { validLogFiles.Add(fi); } } } } ShowLogEntries(validLogFiles.Distinct().OrderBy(a=>a.Name), filter); } private void ShowLogEntries(IEnumerable<FileInfo> files, string filter) { foreach (FileInfo fileInfo in files) { Dump(""); Dump("Reading: ", fileInfo.Name); string content = FileUtil.ReadFromFile(fileInfo.FullName); string[] filters = filter.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries); int lineNumber = 0; foreach (string line in content.Split(new [] {'\r', '\n'})) { foreach (string filterPart in filters) { if (line.Contains(filterPart)) { Dump(lineNumber, "--", line); } } lineNumber++; } Dump("------------------------------------------------------"); } } private void Dump(params object[] values) { ResultTextBox.Text += String.Concat(values) + "\r\n"; } } } |