Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How to log an error through out full application

How to log an error through out full application

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anandkumar Prajapati
    wrote on last edited by
    #1

    I want to print a log whenever an exception is thrown by try-catch block through out whole application.

    P Richard DeemingR 2 Replies Last reply
    0
    • A Anandkumar Prajapati

      I want to print a log whenever an exception is thrown by try-catch block through out whole application.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      What type of application? WPF? WinForms? Xamarin? ASP.NET?

      This space for rent

      1 Reply Last reply
      0
      • A Anandkumar Prajapati

        I want to print a log whenever an exception is thrown by try-catch block through out whole application.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        A try..catch block doesn't throw exceptions; it's used to handle them. If you want to log all unhandled exceptions, then the code you need will vary depending on what type of application you're writing. You could start with the AppDomain.UnhandledException event[^] and the TaskScheduler.UnobservedTaskException event[^]. If you really want to log every exception, even those which are handled, then you'll need to subscribe to the AppDomain.FirstChanceException event[^]. NB: This will generate a lot of noise. You will be logging every exception thrown in any code, including .NET itself, regardless of whether or not it's handled.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        Richard Andrew x64R 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          A try..catch block doesn't throw exceptions; it's used to handle them. If you want to log all unhandled exceptions, then the code you need will vary depending on what type of application you're writing. You could start with the AppDomain.UnhandledException event[^] and the TaskScheduler.UnobservedTaskException event[^]. If you really want to log every exception, even those which are handled, then you'll need to subscribe to the AppDomain.FirstChanceException event[^]. NB: This will generate a lot of noise. You will be logging every exception thrown in any code, including .NET itself, regardless of whether or not it's handled.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Richard Deeming wrote:

          the AppDomain.FirstChanceException event[^]. NB: This will generate a lot of noise.

          Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?

          The difficult we do right away... ...the impossible takes slightly longer.

          L Richard DeemingR 2 Replies Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Richard Deeming wrote:

            the AppDomain.FirstChanceException event[^]. NB: This will generate a lot of noise.

            Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?

            The difficult we do right away... ...the impossible takes slightly longer.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I'm running it full time ("first chance") in one of my UWP apps and have seen no "noise" or slowdown. UWP runs differently than WPF (infrastructure) and I found I needed it in a few cases where my "local" exception handling missed things. (Or maybe I have fewer exceptions). No noise. (I have a breakpoint in the first chance routine so I'm aware of how often it runs.)

            The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.' ― Confucian Analects

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Richard Deeming wrote:

              the AppDomain.FirstChanceException event[^]. NB: This will generate a lot of noise.

              Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?

              The difficult we do right away... ...the impossible takes slightly longer.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Unfortunately, I don't. I've never actually used that event. But I think Visual Studio uses something similar - the output window usually shows first-chance exceptions in the log when you're debugging.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups