Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6705457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:24:22+00:00 2026-05-26T07:24:22+00:00

I am trying to wrap my head around .NET’s code contract. When using code

  • 0

I am trying to wrap my head around .NET’s code contract. When using code contract, how do I log the assert causing the method in question to fail. For example, look at this code:

  Public IsMatch(string x, string, y) 
{ 

    Contract.Assert(!string.IsNullOrWhiteSpace(x)); 
     Contract.Assert(!String.IsNullOrWhitespace(y));

       return x == y;
 }

Let say x = string.Empty. This will cause the method to fail. How do I log that the assert cause the method above to fail. I can have many asserts such as this in a large app and it would be nice to know which one is the trouble-maker.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:24:23+00:00Added an answer on May 26, 2026 at 7:24 am

    When using code contract, how do I log the assert causing the method in question to fail?

    If you’re talking about logging, I assume you mean at runtime rather than static verification.

    The asserts raise exceptions when they fail. You can write a try/catch block to log exceptions:

    try
    {
        bool result = SomeClass.IsMatch(x, y);
    }
    catch(Exception e)
    {
        logger.Error(e.ToString()); // Use whatever logging mechanism here
        throw; // Note: Make sure you rethrow if you catch "Exception e"
    }
    

    This will get you the stack trace of the exception, which will point to the line where the assertion failed.

    As Polity mentioned, you can also include a userMessage parameter on each assertion. That way your logs will contain custom error messages in addition to the stack trace.

    If you need a good logger class, look at the NLog library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wrap my head around asp.net. I have a background as a
I'm trying to wrap my head around custom paging in the ASP.NET Gridview, but
I am trying to wrap my head around the feasability of parsing a log
Im trying to wrap my head around this, using Jquery I want to browse
I am using Delphi2010 and trying to wrap my head around using VirtualStringTree. I've
Trying to wrap my head around updating UI controls from other threads. Currently using
Trying to wrap my head around the jQuery .not() function, and running into a
Im trying to wrap my head around MVVM. I understand a lot of it,
I'm trying to wrap my head around reflection, so I decided to add plugin
I'm trying to wrap my head around creating a toolbar (a tool band in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.