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

Glossary Item Box

Provides an abstraction api for logging to various outputs. This class in the global namespace for a reason. Since you may want to change log infrastructure at any time, it's important that no 'Using' statements are required to access the log infrastructure. Secondly, a static class api can provide improved performance over other possible types. We will be forcing the default configuration of log4net rather than requiring each component to independently configure itself.

Syntax

Visual Basic (Declaration) 
Public MustInherit NotInheritable Class Log 
C# 
public static class Log 

Example

Log/Test/NegativeTests.cs

C#Copy Code
Log.LogWrite += BreakIn_LogWrite;
try
{

    string message = "Bad {500} Data: {0}";
    Exception myError = i_blow_up;
    LogEventArgs arg1 = null;

    LogEventHandler eh = new LogEventHandler(delegate(object s, LogEventArgs e) { arg1 = e; });
    Log.LogWrite += eh;
    Log.LogWrite += BreakIn_LogWrite;
    Log.Error(myError, message, i_blow_up);
    Log.LogWrite -= eh;
    Log.LogWrite -= BreakIn_LogWrite;

    Assert.IsNotNull(arg1);
    Assert.AreEqual(1, arg1.Count);
    Assert.AreEqual(1, arg1.ToArray().Length);

    EventData data = arg1.ToArray()[0];
    Assert.IsNotNull(data);
    BasicLogTest.AssertMessage(GetType(), null, data, LogLevels.Error, null, myError.GetType());

    Assert.IsTrue(data.ToString().Contains(message));
    Assert.IsTrue(data.ToString("{Message:%s!} {}").Contains(message));
    Assert.IsTrue(data.ToString("{Exception}").Contains(myError.GetType().FullName));

    data.Write((System.Xml.XmlTextWriter)null);
    System.Xml.XmlTextWriter wtr = new System.Xml.XmlTextWriter(new MemoryStream(new byte[10]), System.Text.Encoding.UTF32);
    data.Write(wtr);

    data.Write(new StreamWriter(new MemoryStream(new byte[10])));
    foreach (string format in badFormats)
        data.Write(new StreamWriter(new MemoryStream(new byte[10])), format);

    BinaryFormatter ser = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();

    ser.Serialize(ms, arg1);
    Assert.Greater((int)ms.Position, 0);

    ms.Position = 0;
    object restored = ser.Deserialize(ms);
    Assert.IsNotNull(restored);
    Assert.AreEqual(typeof(LogEventArgs), restored.GetType());
    LogEventArgs arg2 = restored as LogEventArgs;

    Assert.IsNotNull(arg2);
    Assert.AreEqual(1, arg2.Count);
    Assert.AreEqual(1, arg2.ToArray().Length);

    data = arg2.ToArray()[0];
    Assert.IsNotNull(data);

    Assert.IsNotNull(data.Exception);
    Assert.AreNotEqual(myError.GetType(), data.Exception.GetType());
    Assert.AreEqual(typeof(Log).Assembly, data.Exception.GetType().Assembly);
    Assert.IsTrue(data.Exception.Message.Contains(myError.GetType().FullName));
    Assert.IsNotNull(data.Exception.ToString());
    Assert.AreNotEqual(String.Empty, data.Exception.ToString());
    Assert.IsTrue(data.Exception.ToString().Contains(myError.GetType().FullName));

    BasicLogTest.AssertMessage(GetType(), null, data, LogLevels.Error, null, data.Exception.GetType());

    System.Runtime.Serialization.SerializationInfo info = new System.Runtime.Serialization.SerializationInfo(data.Exception.GetType(), new myconverter());
    System.Runtime.Serialization.StreamingContext ctx = new System.Runtime.Serialization.StreamingContext();

    Exception err = (Exception)
        data.Exception.GetType().InvokeMember(null, System.Reflection.BindingFlags.CreateInstance,
        null, null, new object[] { info, ctx });

    Assert.IsNotNull(err.Message);
    Assert.IsNotEmpty(err.Message);
    Assert.IsNotNull(err.ToString());
    Assert.IsNotEmpty(err.ToString());
}
finally
{
    Log.LogWrite -= BreakIn_LogWrite;
    Log.LogWrite -= BreakIn_LogWrite;
    Log.LogWrite -= BreakIn_LogWrite;
}
VB.NETCopy Code
Log.LogWrite += BreakIn_LogWrite
Try

    Dim message As String = "Bad {500} Data: {0}"
    Dim myError As Exception = i_blow_up
    Dim arg1 As LogEventArgs = Nothing

    Dim eh As New LogEventHandler(Function(s As Object, e As LogEventArgs) Do
        arg1 = e
    End Function)
    Log.LogWrite += eh
    Log.LogWrite += BreakIn_LogWrite
    Log.[Error](myError, message, i_blow_up)
    Log.LogWrite -= eh
    Log.LogWrite -= BreakIn_LogWrite

    Assert.IsNotNull(arg1)
    Assert.AreEqual(1, arg1.Count)
    Assert.AreEqual(1, arg1.ToArray().Length)

    Dim data As EventData = arg1.ToArray()(0)
    Assert.IsNotNull(data)
    BasicLogTest.AssertMessage([GetType](), Nothing, data, LogLevels.[Error], Nothing, myError.[GetType]())

    Assert.IsTrue(data.ToString().Contains(message))
    Assert.IsTrue(data.ToString("{Message:%s!} {}").Contains(message))
    Assert.IsTrue(data.ToString("{Exception}").Contains(myError.[GetType]().FullName))

    data.Write(DirectCast(Nothing, System.Xml.XmlTextWriter))
    Dim wtr As New System.Xml.XmlTextWriter(New MemoryStream(New Byte(10) {}), System.Text.Encoding.UTF32)
    data.Write(wtr)

    data.Write(New StreamWriter(New MemoryStream(New Byte(10) {})))
    For Each format As String In badFormats
        data.Write(New StreamWriter(New MemoryStream(New Byte(10) {})), format)
    Next

    Dim ser As New BinaryFormatter()
    Dim ms As New MemoryStream()

    ser.Serialize(ms, arg1)
    Assert.Greater(DirectCast(ms.Position, Integer), 0)

    ms.Position = 0
    Dim restored As Object = ser.Deserialize(ms)
    Assert.IsNotNull(restored)
    Assert.AreEqual(GetType(LogEventArgs), restored.[GetType]())
    Dim arg2 As LogEventArgs = TryCast(restored, LogEventArgs)

    Assert.IsNotNull(arg2)
    Assert.AreEqual(1, arg2.Count)
    Assert.AreEqual(1, arg2.ToArray().Length)

    data = arg2.ToArray()(0)
    Assert.IsNotNull(data)

    Assert.IsNotNull(data.Exception)
    Assert.AreNotEqual(myError.[GetType](), data.Exception.[GetType]())
    Assert.AreEqual(GetType(Log).Assembly, data.Exception.[GetType]().Assembly)
    Assert.IsTrue(data.Exception.Message.Contains(myError.[GetType]().FullName))
    Assert.IsNotNull(data.Exception.ToString())
    Assert.AreNotEqual([String].Empty, data.Exception.ToString())
    Assert.IsTrue(data.Exception.ToString().Contains(myError.[GetType]().FullName))

    BasicLogTest.AssertMessage([GetType](), Nothing, data, LogLevels.[Error], Nothing, data.Exception.[GetType]())

    Dim info As New System.Runtime.Serialization.SerializationInfo(data.Exception.[GetType](), New myconverter())
    Dim ctx As New System.Runtime.Serialization.StreamingContext()

    Dim err As Exception = DirectCast(data.Exception.[GetType]().InvokeMember(Nothing, System.Reflection.BindingFlags.CreateInstance, Nothing, Nothing, New Object() {info, ctx}), Exception)

    Assert.IsNotNull(err.Message)
    Assert.IsNotEmpty(err.Message)
    Assert.IsNotNull(err.ToString())
    Assert.IsNotEmpty(err.ToString())
Finally
    Log.LogWrite -= BreakIn_LogWrite
    Log.LogWrite -= BreakIn_LogWrite
    Log.LogWrite -= BreakIn_LogWrite
End Try

Inheritance Hierarchy

System.Object
   Log

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys