By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,054 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,054 IT Pros & Developers. It's quick & easy.

Writing Assertions to a log file?

P: n/a
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

Sep 25 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Brett Romero wrote:
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
http://msdn2.microsoft.com/en-us/library/ty5e4c4h.aspx
http://msdn2.microsoft.com/en-us/library/76dt1k3h.aspx

HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Sep 25 '06 #2

P: n/a
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

Sep 25 '06 #3

P: n/a
Brett Romero wrote:
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
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Sep 25 '06 #4

P: n/a
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

Sep 25 '06 #5

P: n/a
....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" <me@privacy.netwrote in message
news:4n************@individual.net...
Brett Romero wrote:
>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
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm

Sep 25 '06 #6

P: n/a
Brett Romero wrote:
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
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Sep 27 '06 #7

P: n/a
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

Sep 28 '06 #8

P: n/a
Sorry, I didn't have the listeners section configured in the config
file.

Brett

Sep 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.