web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

tracingService vs Exception handling

(0) ShareShare
ReportReport
Posted on by 363

Hi,

I'm looking at this code to learn plugins and programming, can you please explain to me what's happening in the catch block (lines 80-81 in the code sample at the link)?

catch (Exception ex)
                {
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                    throw;
                }

What does the {0} inside the interpolated string stand for  / point to?
Why do I need the Throw instruction here (line 4 or line 81 in the code sample at the link)?
Where can I find the tracingService logs on Dynamics CRM online?

I have the same question (0)
  • Verified answer
    Tony Balogh Profile Picture
    45 on at

    I guess there's quite a bit happening behind the scenes here, for someone new to Dynamics. 

    Line 3: 

    The trace method is defined on the ITracingService interface (ITracing Service documentation), and works similarly to how Console.WriteLine works where the second param is actually an array of params that are intended to be outputted to the trace target. 

    Line 4:

    In demo code, this is likely to illustrate the exception being fired. There may be a global exception handler that will catch this exception for a more generic response.

    All trace messages are found in the administrative part of the site under Plugin Trace Logs. Instructions on how to get to this section can be found here: https://docs.microsoft.com/en-us/powerapps/developer/data-platform/logging-tracing

    pastedimage1619535174175v1.png

    I hope this helps!

  • Verified answer
    Esteban Coto Alfaro Profile Picture
    on at

    Hello:
    1) The {0} point to ex.ToString()

    2) In C #, the word "throw" is a keyword and it is useful to throw an exception manually during the execution of the program. The throw keyword will raise only the exceptions that are derived from the Exception base class.

    3) Here is the ITracingService documentation: docs.microsoft.com/.../microsoft.xrm.sdk.itracingservice


    Thanks!

    Community Support Team - Esteban
    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

  • Joel D Profile Picture
    363 on at

    Thank you for your answers.

    So the {0} points to ex.String(), but I can't understand how that syntax/logic work... Is it supposed to be an interpolated string even if it misses the '$' ?

    Can you please link some examples/documentation of that type of syntax usage?

    Thanks!

  • Tony Balogh Profile Picture
    45 on at

    Console.WriteLine was around before interpolated strings was a feature in C# since ,NET version 1.1 if I recall correctly. Interpolated strings was introduced somewhere in the C# 4.0 to 6.0 versions (I'm going off of memory). Using the trace service's Trace method, you can absolutely bypass this syntax and just use interpolated strings instead, as most people are doing. 

    However, because you asked, here's a breakdown of the TraceWriter.Trace/Console.WriteLine/String.Format syntax which appears to all use String.Format underneath the hood. Links to String.Format documentation which will go through the various possibilities: https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0

    Take a statement like this:

    var cloudCondition = "Sunny";
    var message = String.Format("The skies today will be {0}.", cloudCondition);

    The first parameter is a string template, informing Format about the general structure of what to output. The {0} syntax within the template informs String.Format to use the parameter to this method in the 0 position (0 based arrays). The first parameter following the string template is the 0 position param (cloudCondition). When format sees the {0}, it will replace it with the value of cloudCondition. 

    You can reuse the params within the template. There's a great example under the "Insert a string" in the link above, so I'll spare the words here. 

    Honestly, use the interpolated strings instead of trying to learn this syntax unless you need special formatting capabilities only offered by the String.Format method. 

    Links:

    Console.WriteLine: https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-5.0

    String.Format: https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0

    I hope this helps!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 73 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans