Customizing The Debug Message Output

Debug Messaging System topic explains how to activate the debug messages in your application. However the default console output has very limited possibilities and can only be used effectively in console applications. To use debug messaging system in any environment db4o gives you an API to redirect debug output to another stream:

c#: 

IConfiguration.SetOut(System.IO.TextWriter)

VB: 

IConfiguration.SetOut(System.IO.TextWriter)

An example below shows how to create and use a log file for debug messages:

DebugExample.cs: SetCarsWithFileOutput
01public static void SetCarsWithFileOutput() 02 { 03 // Create StreamWriter for a file 04 FileInfo f = new FileInfo("Debug.txt"); 05 StreamWriter debugWriter = f.CreateText(); 06 07 // Redirect debug output to the specified writer 08 Db4oFactory.Configure().SetOut(debugWriter); 09 10 // Set the debug message levet to the maximum 11 Db4oFactory.Configure().MessageLevel(3); 12 // Do some db4o operations 13 File.Delete(YapFileName); 14 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 15 try 16 { 17 Car car1 = new Car("BMW"); 18 db.Set(car1); 19 Car car2 = new Car("Ferrari"); 20 db.Set(car2); 21 db.Deactivate(car1, 2); 22 IQuery query = db.Query(); 23 query.Constrain(typeof(Car)); 24 IObjectSet results = query.Execute(); 25 ListResult(results); 26 } 27 finally 28 { 29 db.Close(); 30 debugWriter.Close(); 31 } 32 Db4oFactory.Configure().MessageLevel(0); 33 }

DebugExample.vb: SetCarsWithFileOutput
01Public Shared Sub SetCarsWithFileOutput() 02 ' Create StreamWriter for a file 03 Dim f As FileInfo = New FileInfo("Debug.txt") 04 Dim debugWriter As StreamWriter = f.CreateText() 05 06 ' Redirect debug output to the specified writer 07 Db4oFactory.Configure().SetOut(debugWriter) 08 09 ' Set the debug message levet to the maximum 10 Db4oFactory.Configure().MessageLevel(3) 11 ' Do some db4o operations 12 File.Delete(YapFileName) 13 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 14 Try 15 Dim car1 As Car = New Car("BMW") 16 db.Set(car1) 17 Dim car2 As Car = New Car("Ferrari") 18 db.Set(car2) 19 db.Deactivate(car1, 2) 20 Dim query As IQuery = db.Query() 21 query.Constrain(GetType(Car)) 22 Dim results As IObjectSet = query.Execute() 23 ListResult(results) 24 Finally 25 db.Close() 26 debugWriter.Close() 27 End Try 28 Db4oFactory.Configure().MessageLevel(0) 29 End Sub

Using a log file for debug messages has several advantages:

  • debug information is available after the application has terminated;
  • console output is not polluted with debug messages;
  • debug information from the clients can be available on the server.

You can always switch back to the default setting using:

c#: 

IConfiguration.SetOut(System.Console.Out)

VB: 

IConfiguration.SetOut(System.Console.Out)