NET Logging Library
Logging to a Binary File
Basic Tasks > Logging to a Binary File

The Binary File Target can be used to log normal messages and also messages that contain binary attachments.  The binary files created can be viewed with the included Log Viewer application.

It has these properties that can be configured:

 

//Example runtime configuration, can also use log.config, app.config, or web.config
Log.ResetConfiguration();   
BinaryFileTarget target = new BinaryFileTarget ("log.bin");       
Log.Config.Targets.Add(target);

Log.Debug("This is a test");

 

//Log a stream attachment
MemoryStream stream = new MemoryStream();
byte[] bytes = UTF8Encoding.UTF8.GetBytes("This is a test");
stream.Write(bytes,0,bytes.Length);
stream.Seek(0, SeekOrigin.Begin);

Log.LogAttachment(LoggingLevel.DEBUG, "Test Message", "Test.txt",stream);

 

//Log an attachment from a file
File.WriteAllText("Test.txt", "This is a test");
Log.LogAttachment(LoggingLevel.DEBUG, "Test Message", "Test.txt");

 

You can specify dynamic paths using these constructs:
 
Data Directory
http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented
|DataDirectory|\mylogfile.txt

Server.MapPath for ASP.NET, Web Services, and WCF
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx
Server.MapPath("~/App_Data")\mylogfile.txt

Special Folders
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
%ApplicationData%\mylogfile.txt
%CommonApplicationData%\mylogfile.txt
%LocalApplicationData%\mylogfile.txt
%MyDocuments%\mylogfile.txt