Writing Assertions to a log file?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I'd like to use Debug.Assert() but output to a log file instead of the
popup window. Can some one provide an example of how that is done? Or
will C# only let you use th popup window?

Thanks,
Brett
 
Thanks. This gives output similar to:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)
at AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at HostProc.RunUsersAssembly()
at ThreadHelper.ThreadStart_Context(Object state)
at ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at ThreadHelper.ThreadStart()



Is there a way to just output just this part:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)

The other information is useless to me. The message, file, and a line
number with current code and caller is all I need.

Thanks,
Brett
 
Brett said:
Thanks. This gives output similar to:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)
at AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at HostProc.RunUsersAssembly()
at ThreadHelper.ThreadStart_Context(Object state)
at ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at ThreadHelper.ThreadStart()



Is there a way to just output just this part:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)

The other information is useless to me. The message, file, and a line
number with current code and caller is all I need.

Thanks,
Brett

Here is some sample code:

using System.Diagnostics;
using System.IO;

namespace ConsoleApplication
{
public class LogListener : DefaultTraceListener
{
public override void Fail(string message, string detailMessage)
{
// needs to pop up UI
if(AssertUiEnabled)
base.Fail(message, detailMessage);

// is a log file configured??
if (LogFileName == null
|| LogFileName == string.Empty)
return;

using (StreamWriter logFile =
new StreamWriter(LogFileName, true))
{
StackTrace stack = new StackTrace();
StackFrame[] frames = stack.GetFrames();

// extract and log what you need....
logFile.WriteLine(stack);
}
}
}
class Program
{
static void Main(string[] args)
{
Debug.Assert(false, "BOOM");
}
}
}

By using the following app.config aou can inject your implemetation:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<clear />
<!--Use full qualified type name-->
<add
name="LogListener"
type="ConsoleApplication.LogListener, ConsoleApplication"
/>
</listeners>
</trace>
<assert assertuienabled="false" logfilename="c:\log.txt"/>
</system.diagnostics>
</configuration>

HTH,
Andy
 
Thanks. I'm guessing if I want to clear the log file before each
execution of the app, I'll need to add that to the LogListener class?

Brett
 
....and the link that tells you to override TraceListener.Fail() and other
details is http://msdn2.microsoft.com/en-us/library/ttcc4x86.aspx


Andreas Mueller said:
Brett said:
Thanks. This gives output similar to:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)
at AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at HostProc.RunUsersAssembly()
at ThreadHelper.ThreadStart_Context(Object state)
at ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at ThreadHelper.ThreadStart()



Is there a way to just output just this part:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
b.Equals(0): False
---- Assert Long Message ----

at Startup.UT() C:\Test\TestConsole\Main.cs(27)
at Startup.Main(String[] args) C:\Test\TestConsole\Main.cs(18)

The other information is useless to me. The message, file, and a line
number with current code and caller is all I need.

Thanks,
Brett

Here is some sample code:

using System.Diagnostics;
using System.IO;

namespace ConsoleApplication
{
public class LogListener : DefaultTraceListener
{
public override void Fail(string message, string detailMessage)
{
// needs to pop up UI
if(AssertUiEnabled)
base.Fail(message, detailMessage);

// is a log file configured??
if (LogFileName == null
|| LogFileName == string.Empty)
return;

using (StreamWriter logFile =
new StreamWriter(LogFileName, true))
{
StackTrace stack = new StackTrace();
StackFrame[] frames = stack.GetFrames();

// extract and log what you need....
logFile.WriteLine(stack);
}
}
}
class Program
{
static void Main(string[] args)
{
Debug.Assert(false, "BOOM");
}
}
}

By using the following app.config aou can inject your implemetation:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<clear />
<!--Use full qualified type name-->
<add
name="LogListener"
type="ConsoleApplication.LogListener, ConsoleApplication"
/>
</listeners>
</trace>
<assert assertuienabled="false" logfilename="c:\log.txt"/>
</system.diagnostics>
</configuration>

HTH,
Andy
 
Brett said:
Thanks. I'm guessing if I want to clear the log file before each
execution of the app, I'll need to add that to the LogListener class?

Brett
Yeah, that should be a good place:

using System.Diagnostics;
using System.IO;

namespace ConsoleApplication
{
public class LogListener : DefaultTraceListener
{
private static bool initialized = false;
public override void Fail(string message, string detailMessage)
{
if(!initialized)
{
if(File.Exists(LogFileName))
File.Delete(LogFileName);
initialized = true;
}
// needs to pop up UI
if(AssertUiEnabled)
base.Fail(message, detailMessage);

// is a log file configured??
if (LogFileName == null
|| LogFileName == string.Empty)
return;

using (StreamWriter logFile =
new StreamWriter(LogFileName, true))
{
StackTrace stack = new StackTrace();
StackFrame[] frames = stack.GetFrames();

// extract and log what you need....
logFile.WriteLine(stack);
}
}
}
class Program
{
static void Main(string[] args)
{
Debug.Assert(false, "BOOM");
Debug.Assert(false, "BOOM");
}
}
}


HTH,
Andy
 
Andreas, what is needed to actually use the custom listener class?
When I call Debug.Assert(), it isn't using the custom listener. I say
that because the log is not cleared and break points in the class
aren't hit.

Thanks,
Brett
 
Sorry, I didn't have the listeners section configured in the config
file.

Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top