c# - Azure Function static constructor error logging -
i have azure function app (using newer .net class library approach) initialize static constructor share generated resources.
official documentation recommends sharing resources httpclient in web api.
the discussion @ bottom of docs on azure functions c# script developer reference mentions placing httpclient in static variable prevent re-instantiation on each request since thread-safe.
i'm wondering 2 things.
is static constructor ok initializing expensive 'set-up' resources used requests?
if approach ok, how should error logging configured in static constructor if initialization of these resources fails?
here class definition
public static class httpsubmissiontrigger { private static readonly sendgridclient sendgridclient; private static readonly func<idictionary<string, object>, string> template; private static readonly emailaddress senderemail; private static readonly string emailtitle; private static readonly httpresponsemessage errorresponse; static httpsubmissiontrigger() { // ... initialization of above static members here } public static async task<httpresponsemessage> run(...) { // ... use static members here send emails, respond client } } i perform error logging in run method using di of tracewriter, works fine. can use view errors in azure portal console function, static constructors can't have parameters, approach won't work resource initialization.
there reference question in azure function docs response ask question here.
you're not limited static constructor perform initialization logic shared resources.
one approach, of few possible, have class managing resources you, you'd perform static initialization when function invoked, passing loggers , other relevant information, ensuring appropriate checks avoid double initialization.
the function filters feature we'll releasing these scenarios well: https://github.com/azure/azure-webjobs-sdk/wiki/function-filters
Comments
Post a Comment