Last active
February 14, 2016 05:49
-
-
Save lolp1/5aec6bda095e36b2235b to your computer and use it in GitHub Desktop.
Log improvements.
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
. | |
public static void Main() | |
{ | |
Logs = new LogManager(); | |
Logs.Add(new LogElement("Program")); | |
bool shouldLog = Randomizer.GenerateNumber(5, 10) >= 7; | |
var logEntry = new LogEntry(LogLevel.Warning, "Argument format string"); | |
ILogElement element = Logs["Sender"]; | |
element.Log("Hi."); | |
element.Log(new LogEntry(LogLevel.Debug, "HiAgain")); | |
element.Log("Log only if condition is met", shouldLog); | |
element.Log(logEntry.Format("args")); | |
var instance = HtmlLog.GetInstance("Path to html file."); | |
var htmlLog = HtmlLog.GetInstance(); | |
htmlLog.Error("Why the default log?! Okay thats fine."); | |
instance.Info("B"); | |
instance.Debug("C"); | |
instance.Warn("D"); | |
instance.Info("E"); | |
instance.Fatal("F"); | |
} | |
public class MyClass : LogElement | |
{ | |
public MyClass() : base("MyLogInstance") | |
{ | |
Log("Hi, default log message here. Level 'Normal' is used".); | |
Log(new LogEntry(LogLevel.Warning, "I can log warnings.. and such to".); | |
var entry = Log(new LogEntry(LogLevel.Warning, "We can use string, object[] args format too", "blah", "blah.."); | |
Log(new LogEntry(entry.Format()); | |
} | |
} | |
// Implementing custom logging Make a quick rich text box log for our scripts and plugins forms. | |
public class RichTextBoxLog : LogElement | |
{ | |
/// <summary> | |
/// The _rich text box | |
/// </summary> | |
private readonly RichTextBox _richTextBox; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="RichTextBoxLog"/> class. | |
/// </summary> | |
/// <param name="sender">The sender.</param> | |
/// <param name="richTextBox">The rich text box.</param> | |
public RichTextBoxLog(string sender, RichTextBox richTextBox) : base(sender) | |
{ | |
_richTextBox = richTextBox; | |
} | |
// Simple override. | |
/// <summary> | |
/// Logs the specified log entry. | |
/// </summary> | |
/// <param name="logEntry">The log entry.</param> | |
/// <exception cref="ArgumentOutOfRangeException"></exception> | |
public override void Log(LogEntry logEntry) | |
{ | |
switch (logEntry.Level) | |
{ | |
case LogLevel.Debug: | |
_richTextBox.AppendMessage(logEntry.Format(Identifier), Color.Black); | |
break; | |
case LogLevel.Warning: | |
_richTextBox.AppendMessage(logEntry.Format(Identifier), Color.MediumVioletRed); | |
break; | |
case LogLevel.Error: | |
_richTextBox.AppendMessage(logEntry.Format(Identifier), Color.Red); | |
break; | |
case LogLevel.Information: | |
_richTextBox.AppendMessage(logEntry.Format(Identifier), Color.Indigo); | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(); | |
} | |
} | |
} | |
// Using it. | |
public partial class DevScriptForm : MetroForm, ILogElement | |
{ | |
private readonly ILogElement _log; | |
public DevScriptForm() | |
{ | |
InitializeComponent(); | |
_log = new RichTextBoxLog("Scripts",scriptTextBoxLog); | |
_log.Log(new LogEntry(LogLevel.Debug,"Hi there.")); | |
} | |
public void Log(LogEntry entry) | |
{ | |
_log.Log(entry); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Messed up file type