CSharpTest.Net
SetOutputFormat Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Logging Assembly > (Global) Namespace > Log.Config Class : SetOutputFormat Method

outputToConfigure
newFormat

Glossary Item Box

Changes the output format used to write to a specific output device. The format of this string behaves just like EventData.ToString(). The string can contain any public field or property available for the CSharpTest.Net.Logging.EventData class surrounded by braces {} and yes, properties/fields are case sensative. The input string should look something like the following examples: "[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}" -- this is the default format of ToString() "{EventTime:o} [{ProcessId:D4},{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}" -- This is the default log file format.

Syntax

Visual Basic (Declaration) 
Public Shared Sub SetOutputFormat( _
   ByVal outputToConfigure As LogOutputs, _
   ByVal newFormat As String _
) 
C# 
public static void SetOutputFormat( 
   LogOutputs outputToConfigure,
   string newFormat
)

Parameters

outputToConfigure
newFormat

Example

Log/Test/NegativeTests.cs

C#Copy Code
string origFile = Log.Config.LogFile;
try
{
    Log.Config.Output |= LogOutputs.LogFile;
    Log.Config.SetOutputFormat(LogOutputs.TraceWrite, "{Message}");

    Log.Config.LogFile = @"\\\\\ { <mal formed!? file> ///{0}.txt";
    Log.Write("Hi1");
    Log.Write("Hi2");
    Log.Write("Hi3");
    Assert.AreEqual(this.GetType().FullName + ": Hi3", _lastTrace);
    Log.Config.LogFile = @"C: mal {formatted} file {0}.txt";
    Log.Write("Hi4");
    Log.Write("Hi5");
    Log.Write("Hi6");
    Assert.AreEqual(this.GetType().FullName + ": Hi6", _lastTrace);
    string path = Path.Combine(Path.GetTempPath(), @"my-path-doesnt-exist");
    if (Directory.Exists(path)) Directory.Delete(path, true);
    Log.Config.LogFile = Path.Combine(path, "log.txt");
    Log.Write("Hi!");
    Assert.IsTrue(Directory.Exists(path));
    Assert.IsTrue(File.Exists(Path.Combine(path, "log.txt")));
    Log.Config.LogFile = origFile;
    Directory.Delete(path, true);

    string dir = Environment.CurrentDirectory;
    Environment.CurrentDirectory = Path.GetDirectoryName(origFile);
    Log.Config.LogFile = Path.GetFileName(origFile);
    Assert.AreEqual(origFile, Log.Config.LogFile);
    Environment.CurrentDirectory = dir;
    Assert.AreEqual(origFile, Log.Config.LogFile);
}
finally
{
    Log.Config.LogFile = origFile;
}
VB.NETCopy Code
Dim origFile As String = Log.Config.LogFile
Try
    Log.Config.Output = Log.Config.Output Or LogOutputs.LogFile
    Log.Config.SetOutputFormat(LogOutputs.TraceWrite, "{Message}")

    Log.Config.LogFile = "\\\\\ { <mal formed!? file> ///{0}.txt"
    Log.Write("Hi1")
    Log.Write("Hi2")
    Log.Write("Hi3")
    Assert.AreEqual(Me.[GetType]().FullName + ": Hi3", _lastTrace)
    Log.Config.LogFile = "C: mal {formatted} file {0}.txt"
    Log.Write("Hi4")
    Log.Write("Hi5")
    Log.Write("Hi6")
    Assert.AreEqual(Me.[GetType]().FullName + ": Hi6", _lastTrace)
    Dim path As String = Path.Combine(Path.GetTempPath(), "my-path-doesnt-exist")
    If Directory.Exists(path) Then
        Directory.Delete(path, True)
    End If
    Log.Config.LogFile = Path.Combine(path, "log.txt")
    Log.Write("Hi!")
    Assert.IsTrue(Directory.Exists(path))
    Assert.IsTrue(File.Exists(Path.Combine(path, "log.txt")))
    Log.Config.LogFile = origFile
    Directory.Delete(path, True)

    Dim dir As String = Environment.CurrentDirectory
    Environment.CurrentDirectory = Path.GetDirectoryName(origFile)
    Log.Config.LogFile = Path.GetFileName(origFile)
    Assert.AreEqual(origFile, Log.Config.LogFile)
    Environment.CurrentDirectory = dir
    Assert.AreEqual(origFile, Log.Config.LogFile)
Finally
    Log.Config.LogFile = origFile
End Try

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys