AXForum  
Go Back   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Forgotten Your Password?
Register Forum Rules FAQ Members List Today's Posts Search Mark Forums Read

 
 
Thread Tools Search this Thread Display Modes
Old 01.08.2018, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,644 / 848 (80) +++++++
Join Date: 28.10.2006
crmtipoftheday: Tip #1145: Tracing in Azure Functions MkIII
Источник: https://crmtipoftheday.com/1145/trac...nctions-mkiii/
==============

This never ends. Shortly after I finished writing about tracing in Azure Functions, I found Daryl “Always Raising” LaBar explaining how to use ExtendedOrganizationService wrapper to easily capture everything in your plugin:
Wouldn’t it be nice for debugging if your plugins automatically traced every IOrganzationService Request, along with the parameters and the time that it took? With the DLaB ExtenededOrganizationService, now you can… https://t.co/x3t2lZmK6N pic.twitter.com/XsegHMh2vZ

— Daryl LaBar (@ddlabar) July 17, 2018

The primary purpose of the tracing wrapper in Azure Functions is to reuse existing functionality you might already have implemented in a separate assemblies. If the existing code uses Trace then my wrapper works nicely redirecting the output to ILogger supplied by the function. But Daryl’s point is that the existing code, if it comes from plugins / workflows would have used ITracingService provided by the execution context. So we need our logger to understand ITracingService as well. Fear not, let me raise the LaBar by standing on his shoulders. We’ll take an extra parameter and implement the interface:

using Microsoft.Extensions.Logging; using Microsoft.Xrm.Sdk; using System.Diagnostics; namespace YourNamespace { // Log writter for working with Azure Functions public class TraceWriterListener : TraceListener, ITracingService { private ILogger _log; private ITracingService _tracer; public TraceWriterListener(string name, ILogger logger, ITracingService tracer = null) : base(name) { _log = logger; _tracer = tracer; } public TraceWriterListener(ILogger logger, ITracingService tracer = null) : base() { _log = logger; _tracer = tracer; } // this is the one we have to overwrite to get // the logging level right public override void TraceEvent( TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) { switch(eventType) { case TraceEventType.Verbose: _log?.LogDebug(message); break; case TraceEventType.Information: _log?.LogInformation(message); break; case TraceEventType.Warning: _log?.LogWarning(message); break; case TraceEventType.Error: _log?.LogError(message); break; case TraceEventType.Critical: _log?.LogCritical(message); break; default:break; } } public override void Write(string message) { _log?.LogTrace(message); _trace?.Trace(message); } public override void WriteLine(string message) { _log?.LogTrace(message); _trace?.Trace(message); } // ITracingService implementation public void Trace(string fmt, params object[] args) { _log?.LogTrace(ftm, args); _trace?.Trace(fmt, args); } } } Now, you can call your existing AwesomeMethod and pass the wrapper in. That way it will get logged as part of the Functions logging as well as go into the trace output of the Dynamics infrastructure.

public static void Run( [TimerTrigger("0 0 0 1 1 *")]TimerInfo myTimer, ILogger log) { // get your tracing service here var tracingService = ...; var logger = new TraceWriterListener( log, tracingService); Trace.Listeners.Add(logger); Trace.TraceInformation($"Information"); Trace.TraceWarning($"Warning"); Trace.TraceError($"Error"); Trace.WriteLine($"Level comes from ILogger"); YourSuperDuperExistingClass .AwesomeMethod(whatever, logger); } (Facebook and Twitter cover photo by Caterina Beleffi on Unsplash)



Источник: https://crmtipoftheday.com/1145/trac...nctions-mkiii/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Similar Threads
Thread Thread Starter Forum Replies Last Post
crmtipoftheday: Tip #1142: Tracing in Azure Functions MkII Blog bot Dynamics CRM: Blogs 0 26.07.2018 14:11
crmtipoftheday: Tip #1112: Impersonate Azure AD users Blog bot Dynamics CRM: Blogs 0 04.05.2018 00:23
crmtipoftheday: Tip #1066: Discover Azure AD tenant ID with Occam’s razor Blog bot Dynamics CRM: Blogs 0 02.02.2018 04:34
Navigate Into Success: Invoking Azure Functions from AL Blog bot NAV: Blogs 0 08.06.2017 15:11
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Рейтинг@Mail.ru
All times are GMT +3. The time now is 10:21.
Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Contacts E-mail, Advertising.