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

DebugExample.vb: SetCarsWithFileOutput
01Private 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(Db4oFileName) 13 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 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)