473,414 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Writing Assertions to a log file?

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
8 3417
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
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
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
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
....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
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
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
Sorry, I didn't have the listeners section configured in the config
file.

Brett

Sep 29 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: post400 | last post by:
Hi, there is another famous book 'Writing solid code' but does it apply to Python ? Or it's usable only by Microsoft C programmers ? The author seems to be an ex-Microsoft guy ! Thanks ,...
0
by: P. Chalin | last post by:
If you make use of assertions (e.g. assert statements) in your code, I would like to invite you to participate in a 15 min survey on program assertions. In this survey we seek your opinion...
9
by: 100 | last post by:
Has anybody read Steve Maguire's book "Writing solid code"? Do you think that the ideas in this book are not applicable in c# language? Does anybody find if(2 == i) istead of if(i == 2) as...
14
by: Divick | last post by:
Hi, is it a nice idea to do assetion checks for method arguments ? I am currently redesigning a piece of code. Now the problem is that there are some assumptions for the method arguments that the...
8
by: Jefffff | last post by:
Regarding the use of assertions: According to Juval Lowy's excellent coding standards document (PDF download available at www.idesign.net), every assumption in your code should have an...
8
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
58
by: jeffc226 | last post by:
This might be less of a design issue than a C++ language issue per se, but I have a problem with assertions. I mean, they work, of course. But there's something I'm uncomfortable with that's never...
1
by: =?iso-8859-1?Q?Daniel_Lidstr=F6m?= | last post by:
(I posted this to m.p.pocketpc.developer but got no answers) Hello! I have a problem when launching my application. It starts with firing an assertion. This assertion is inside mfc or atl it...
3
by: Hugh Oxford | last post by:
I am told that I should use assertions in my testing. The problem is, because I test using a cut of live data, the data always changes. Has anyone come up against this or have they abandoned...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.