Home > Products > LucidLog.Net > How to ...
 

LucidLog.Net - How To...

How to write a simple log entry
How to set a category for a log entry
How to log objects
How to load the configuration file
How to configure the library programmatically
How to set various targets for log entries
How to mark the beginning/end of a method
How to reassign the System.Diagnostics.Trace output
How to disable log entries with certain categories, severity and types

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#]
  // Writes information about a named execution point of the program to the log.
  DRLog.Logger.Checkpoint("Checkpoint #1");
  
  ArrayList arrList = new ArrayList();
  arrList.Add("Hello");
  arrList.Add("World");
  arrList.Add("!");
  // Writes a message and all elements of the list to the log
  DRLog.Logger.LogList("Log IList object", arrList);	  
  
  [Visual Basic]
  // Writes information about a named execution point of the program to the log.
  DRLog.Logger.Checkpoint("Checkpoint #1")
    
  Dim arrList As New ArrayList()
  arrList.Add("Hello")
  arrList.Add("World")
  arrList.Add("!")
  'Writes a message and all elements of the list to the log  
  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);
  // Writes a message, a Rectangle object, a category name to log.
  DRLog.Logger.Log("Logging Rectangle object",r,"Ctg:Sample object's logging");
  		
  Form f = new Form();
  f.Size = new System.Drawing.Size(250,300);
  // Writes a message, a Form object, a category name to log.
  DRLog.Logger.Log("Logging Form object",f,"Ctg:Sample object's logging");
 }
  
  [Visual Basic]
  Private Sub LogObjects()
    ' Writes a message, a Rectangle object, a category name to log.
    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")
    			
    'Writes a message, a Form object, a category name to log.
    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#]
  //You should specify the path to the configuration file as a parameter
  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;
  
  //Set the folder location for a log files
  config.File.Folder =  @"C:\Logs\";  
  config.File.Name = "MyLog";
  config.File.LogFileMode = LogFileMode.Overwrite;
  
  //Assign the caption of the MessageBox which display of your log messages.
  config.MsgBox.Caption = "My application Error";
  
  [Visual Basic]
  Dim config As ILoggerConfig = DRLog.Logger.Config
  config.Enabled = True
  
  'Set the folder location for a log files
  config.File.Folder = "C:\Logs\"
  config.File.Name = "MyLog"
  config.File.LogFileMode = LogFileMode.Overwrite
   
  'Assign the caption of the MessageBox which display of your log messages.
  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;
  /* adds the File as a target for all log entries with 
    the severity starting from Message */
  config.SetTargetsStartingWith(LogEntrySeverity.Message, TargetType.File);
  
  /* adds also the Viewer to the already specified target 
    for all log entries with the severity starting from Warning */
  config.SetTargetsStartingWith(LogEntrySeverity.Warning, TargetType.Viewer);
  
  /* adds also the EventLog to the already specified 
    targets for log entries with the Fatal severity */
  config.SetTargets(LogEntrySeverity.Fatal, TargetType.EventLog);  
  
  [Visual Basic]
  Dim config As ILoggerConfig = DRLog.Logger.Config
  ' adds the File as a target for all log entries with 
  ' the severity starting from Message 
  config.SetTargetsStartingWith(LogEntrySeverity.Message, TargetType.File)
  
  'adds also the Viewer to the already specified target 
  'for all log entries with the severity starting from Warning
  config.SetTargetsStartingWith(LogEntrySeverity.Warning, TargetType.Viewer)
  
  'adds also the EventLog to the already specified
  'targets for log entries with the Fatal severity
  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>
  <!-- adds the File as a target for all log entries with the severity starting from Message --> 
  <rule name="Default rule" enable="true" sev="all" targets="File" />
  	
  <!-- adds also the Viewer to the already specified target for all log entries with the severity starting from Warning --> 
  <rule name="WarningsAndErrors" enable="true" area="StartingWith" sev="Warning" targets="Viewer" />
  	
  <!-- adds also the EventLog to the already specified targets for log entries with the Fatal severity -->
  <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");     
    // other log code go here
    DRLog.Logger.LeaveMethod("MyMethod");
  }
  
  [Visual Basic]
  Private Sub Method2()
    DRLog.Logger.EnterMethod("MyMethod")    
    // other log code go here  		
    DRLog.Logger.LeaveMethod("MyMethod")
  End Sub    

How to reassign the System.Diagnostics.Trace output

  [C#]
  ILoggerConfig config = DRLog.Logger.Config; 
  config.ReassignSDTrace = true;
  //let us assign the file and the LogViewer as targets
  config.SetSDTraceTargets(TargetType.File, TargetType.Viewer);  
  
  [Visual Basic]
  Dim config As ILoggerConfig = DRLog.Logger.Config
  config.ReassignSDTrace = True
  //let us assign the file and the LogViewer as targets
  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" />