LucidLog.Net - How To...
How to write a simple log entry
Write the following code in order to send a simple log entry:
[C#]
DRLog.Logger.Log("My first LogEntry");
[Visual Basic]
DRLog.Logger.Log("My first LogEntry")
How to set a category for a log entry
The following syntax is used to define category:
[C#]
DRLog.Logger.Log("Written using My Category","My Category");
[Visual Basic]
DRLog.Logger.Log("Written using My Category","My Category")
How to log objects
1. Example of logging objects of several predefined types.
[C#]
DRLog.Logger.Checkpoint("Checkpoint #1");
ArrayList arrList = new ArrayList();
arrList.Add("Hello");
arrList.Add("World");
arrList.Add("!");
DRLog.Logger.LogList("Log IList object", arrList);
[Visual Basic]
DRLog.Logger.Checkpoint("Checkpoint #1")
Dim arrList As New ArrayList()
arrList.Add("Hello")
arrList.Add("World")
arrList.Add("!")
DRLog.Logger.LogList("Log IList object", arrList)
2. Example of logging objects of any types.
This methods of LucidLog.Net library writes to log all public properties and fields of the object.
[C#]
private void LogObjects()
{
System.Drawing.Rectangle r = new System.Drawing.Rectangle(10,10,100,200);
DRLog.Logger.Log("Logging Rectangle object",r,"Ctg:Sample object's logging");
Form f = new Form();
f.Size = new System.Drawing.Size(250,300);
DRLog.Logger.Log("Logging Form object",f,"Ctg:Sample object's logging");
}
[Visual Basic]
Private Sub LogObjects()
r As System.Drawing.Rectangle = New System.Drawing.Rectangle(10,10,100,200)
DRLog.Logger.Log("Logging Rectangle object",r,"Ctg:Sample object's logging")
f As System.Windows.Form = New Form()
f.Size = New System.Drawing.Size(250,300)
DRLog.Logger.Log("Logging Form object",f,"Ctg:Sample object's logging")
End Sub
How to load the configuration file
The following syntax is used to load configuration from file:
[C#]
DRLog.LoadConfiguration(@"C:\Logs\config.drlcfg");
[Visual Basic]
'You should specify the path to the configuration file as a parameter
DRLog.LoadConfiguration("C:\Logs\config.drlcfg")
How to configure the library programmatically
[C#]
ILoggerConfig config = DRLog.Logger.Config;
config.Enabled = True;
config.File.Folder = @"C:\Logs\";
config.File.Name = "MyLog";
config.File.LogFileMode = LogFileMode.Overwrite;
config.MsgBox.Caption = "My application Error";
[Visual Basic]
Dim config As ILoggerConfig = DRLog.Logger.Config
config.Enabled = True
config.File.Folder = "C:\Logs\"
config.File.Name = "MyLog"
config.File.LogFileMode = LogFileMode.Overwrite
config.MsgBox.Caption = "My application Error"
How to set various targets for log entries
The following syntax is used to define targets:
[C#]
ILoggerConfig config = DRLog.Logger.Config;
config.SetTargetsStartingWith(LogEntrySeverity.Message, TargetType.File);
config.SetTargetsStartingWith(LogEntrySeverity.Warning, TargetType.Viewer);
config.SetTargets(LogEntrySeverity.Fatal, TargetType.EventLog);
[Visual Basic]
Dim config As ILoggerConfig = DRLog.Logger.Config
config.SetTargetsStartingWith(LogEntrySeverity.Message, TargetType.File)
config.SetTargetsStartingWith(LogEntrySeverity.Warning, TargetType.Viewer)
config.SetTargets(LogEntrySeverity.Fatal, TargetType.EventLog)
Using the configuration file:
The enable = true|false attribute allows/forbids each of the rules to be used.
<targetrules>
<rule name="Default rule" enable="true" sev="all" targets="File" />
<rule name="WarningsAndErrors" enable="true" area="StartingWith" sev="Warning" targets="Viewer" />
<rule name="OnlyError" enable="true" area="OnlyFor" sev="Fatal" targets="EventLog" />
</targetrules>
How to mark the beginning/end of a method
1. The name of the method is detected automatically.
[C#]
private void Method1()
{
DRLog.Logger.EnterMethod();
DRLog.Logger.Log("Log information");
DRLog.Logger.LeaveMethod();
}
[Visual Basic]
Private Sub Method1()
DRLog.Logger.EnterMethod()
DRLog.Logger.Log("Log information")
DRLog.Logger.LeaveMethod()
End Sub
2. The user defines the name of the method
[C#]
private void Method2()
{
DRLog.Logger.EnterMethod("MyMethod");
DRLog.Logger.LeaveMethod("MyMethod");
}
[Visual Basic]
Private Sub Method2()
DRLog.Logger.EnterMethod("MyMethod")
DRLog.Logger.LeaveMethod("MyMethod")
End Sub
How to reassign the System.Diagnostics.Trace output
[C#]
ILoggerConfig config = DRLog.Logger.Config;
config.ReassignSDTrace = true;
config.SetSDTraceTargets(TargetType.File, TargetType.Viewer);
[Visual Basic]
Dim config As ILoggerConfig = DRLog.Logger.Config
config.ReassignSDTrace = True
config.SetSDTraceTargets(TargetType.File, TargetType.Viewer)
Using the configuration file:
<general enable="true" stacktrace="true" reassignsystemtrace="true" targets="File, Viewer" internallogenable="false" />
How to disable log entries with certain categories, severity and types
In this example, if all 3 methods are called, all log entries from categories "Category A" and "Category B", all log entries with the Message severity and all log entries of the Checkpoint type will be ignored. It is important to remember that all rules are summarized.
1. To disable logging for categories "Category A" and "Category B", use the following code:
[C#]
DRLog.Logger.Config.DisableCategories("Category A", "Category B");
[Visual Basic]
DRLog.Logger.Config.DisableCategories("Category A", "Category B")
2. To disable logging for log entries with severity Message, use the following code:
[C#]
DRLog.Logger.Config.DisableSeverities(LogEntrySeverity.Message);
[Visual Basic]
DRLog.Logger.Config.DisableSeverities(LogEntrySeverity.Message)
3. To disable logging for log entries of the Checkpoint and SystemInfo types, use the following code:
[C#]
DRLog.Logger.Config.DisableLogEntryTypes(LogEntryType.Checkpoint, LogEntryType.SystemInfo );
[Visual Basic]
DRLog.Logger.Config.DisableLogEntryTypes(LogEntryType.Checkpoint, LogEntryType.SystemInfo)
You can do the same using the configuration file. Use the following code:
<advanced categories="Category A, Category B" severities="Message" types="Checkpoint, SystemInfo" />
|