//public object StartNewActivity(string operationName)
            //{
            //    invoker = (operationName) =>
            //    {
            //        Activity activity = new Activity(operationName);
            //        ActivityStub activityStub = ActivityStub.Wrap(activity);
            //        PreStartInitializationCallback(activityStub);
            //        autoInstrumentationDiagnosticSource.StartActivity(actvitiy, activity);
            //    }
            //}

            public void AddBaggage(object activityInstance, string key, string value)
            {
                // Activity API signature:
                // public Activity AddBaggage(string key, string value)

                // invoker = (activityInstance, key, value) => ((Activity) activityInstance).AddBaggage(key, value);

                ValidateType(activityInstance);

                Action <object, string, string> invoker = _cashedDelegates.AddBagage;

                if (invoker == null)
                {
                    ParameterExpression exprActivityInstance = Expression.Parameter(_activityType, "activityInstance");
                    ParameterExpression exprKey   = Expression.Parameter(typeof(string), "key");
                    ParameterExpression exprValue = Expression.Parameter(typeof(string), "value");

                    var exprInvoker = Expression.Lambda <Action <object, string, string> >(
                        Expression.Call(
                            Expression.Convert(exprActivityInstance, _activityType),
                            "AddBaggage", new[] { typeof(string), typeof(string) }, exprKey, exprValue),
                        exprActivityInstance, exprKey, exprValue);

                    invoker = exprInvoker.Compile();
                    invoker = Concurrent.TrySetOrGetValue(ref _cashedDelegates.AddBagage, invoker);
                }

                invoker(activityInstance, key, value);
            }
            public string GetBaggageItem(object activityInstance, string key)
            {
                // Activity API signature:
                // public string GetBaggageItem(string key)

                // invoker = (activityInstance, key) => ((Activity) activityInstance).GetBaggageItem(key);

                ValidateType(activityInstance);

                Func <object, string, string> invoker = _cashedDelegates.GetBaggageItem;

                if (invoker == null)
                {
                    ParameterExpression exprActivityInstance = Expression.Parameter(_activityType, "activityInstance");
                    ParameterExpression exprKey = Expression.Parameter(typeof(string), "key");

                    var exprInvoker = Expression.Lambda <Func <object, string, string> >(
                        Expression.Call(
                            Expression.Convert(exprActivityInstance, _activityType),
                            "GetBaggageItem", new[] { typeof(string) }, exprKey),
                        exprActivityInstance, exprKey);

                    invoker = exprInvoker.Compile();
                    invoker = Concurrent.TrySetOrGetValue(ref _cashedDelegates.GetBaggageItem, invoker);
                }

                string result = invoker(activityInstance, key);

                return(result);
            }
            public IEnumerable <KeyValuePair <string, string> > get_Baggage(object activityInstance)
            {
                // Activity API signature:
                // public IEnumerable<KeyValuePair<string, string>> Baggage { get; }

                // invoker = (activityInstance) => ((Activity) activityInstance).Baggage;

                ValidateType(activityInstance);

                Func <object, IEnumerable <KeyValuePair <string, string> > > invoker = _cashedDelegates.get_Baggage;

                if (invoker == null)
                {
                    ParameterExpression exprActivityInstance = Expression.Parameter(_activityType, "activityInstance");

                    var exprInvoker = Expression.Lambda <Func <object, IEnumerable <KeyValuePair <string, string> > > >(
                        Expression.Property(
                            Expression.Convert(exprActivityInstance, _activityType),
                            "Baggage"),
                        exprActivityInstance);

                    invoker = exprInvoker.Compile();
                    invoker = Concurrent.TrySetOrGetValue(ref _cashedDelegates.get_Baggage, invoker);
                }

                IEnumerable <KeyValuePair <string, string> > result = invoker(activityInstance);

                return(result);
            }
            public string get_Name(object diagnosticListenerInstance)
            {
                Func <object, string> invokerDelegate = _cashedDelegates.get_Name;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = (diagnosticListenerInstance) => ((DiagnosticListener) diagnosticListenerInstance).Name;

                        ParameterExpression exprDiagnosticListenerInstanceParam = Expression.Parameter(typeof(object), "diagnosticListenerInstance");

                        PropertyInfo propertyInfo = _thisInvoker._diagnosticListenerType.GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);

                        var exprInvoker = Expression.Lambda <Func <object, string> >(
                            Expression.Property(
                                Expression.Convert(exprDiagnosticListenerInstanceParam, _thisInvoker._diagnosticListenerType),
                                propertyInfo),
                            exprDiagnosticListenerInstanceParam);

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.get_Name, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticListener),
                                                             $"Error while building the invocation delegate for the API \"{nameof(get_Name)}\".",
                                                             ex);
                    }
                }

                string result = invokerDelegate(diagnosticListenerInstance);

                return(result);
            }
            public IObservable <object> get_AllListeners()
            {
                Func <IObservable <object> > invokerDelegate = _cashedDelegates.get_AllListeners;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = () => DiagnosticListener.AllListeners;

                        PropertyInfo propertyInfo = _thisInvoker._diagnosticListenerType.GetProperty("AllListeners", BindingFlags.Static | BindingFlags.Public);

                        var exprInvoker = Expression.Lambda <Func <IObservable <object> > >(
                            Expression.Property(
                                null,
                                propertyInfo));

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.get_AllListeners, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticListener),
                                                             $"Error while building the invocation delegate for the API \"{nameof(get_AllListeners)}\".",
                                                             ex);
                    }
                }

                IObservable <object> result = invokerDelegate();

                return(result);
            }
            public object Ctor(string diagnosticSourceName)
            {
                Func <string, object> invokerDelegate = _cashedDelegates.Ctor;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = (diagnosticSourceName) => new DiagnosticListener(diagnosticSourceName);

                        ParameterExpression exprDiagnosticSourceNameParam = Expression.Parameter(typeof(string), "diagnosticSourceName");

                        ConstructorInfo ctorInfo = _thisInvoker._diagnosticListenerType.GetConstructor(new Type[] { typeof(string) });

                        var exprInvoker = Expression.Lambda <Func <string, object> >(
                            Expression.New(
                                ctorInfo,
                                exprDiagnosticSourceNameParam),
                            exprDiagnosticSourceNameParam);

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.Ctor, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticListener),
                                                             $"Error while building the invocation delegate for the API \"{nameof(Ctor)}\".",
                                                             ex);
                    }
                }

                object result = invokerDelegate(diagnosticSourceName);

                return(result);
            }
Exemplo n.º 7
0
        private static PackagedAssemblyLookup GetPackagedAssemblies()
        {
            PackagedAssemblyLookup packagedAssemblies = s_packagedAssemblies;

            if (packagedAssemblies == null)
            {
                packagedAssemblies = ReadPackagedAssembliesFromDisk();
                packagedAssemblies = Concurrent.TrySetOrGetValue(ref s_packagedAssemblies, packagedAssemblies);
            }

            return(packagedAssemblies);
        }
            public bool IsEnabled(object diagnosticSourceInstance, string eventName, object arg1, object arg2)
            {
                Func <object, string, object, object, bool> invokerDelegate = _cashedDelegates.IsEnabled;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = (diagnosticSourceInstance, eventName, arg1, arg2) =>
                        //                              ((Diagnosticource) diagnosticSourceInstance).Subscribe(eventName, arg1, arg2);

                        ParameterExpression exprDiagnosticSourceInstanceParam = Expression.Parameter(typeof(object), "diagnosticSourceInstance");
                        ParameterExpression exprEventNameParam = Expression.Parameter(typeof(string), "eventName");
                        ParameterExpression exprArg1Param      = Expression.Parameter(typeof(object), "arg1");
                        ParameterExpression exprArg2Param      = Expression.Parameter(typeof(object), "arg2");

                        MethodInfo methodInfo = _thisInvoker._diagnosticSourceType.GetMethod("IsEnabled",
                                                                                             BindingFlags.Instance | BindingFlags.Public,
                                                                                             binder: null,
                                                                                             new Type[] { typeof(string), typeof(object), typeof(object) },
                                                                                             modifiers: null);

                        var exprInvoker = Expression.Lambda <Func <object, string, object, object, bool> >(
                            Expression.Call(
                                Expression.Convert(exprDiagnosticSourceInstanceParam, _thisInvoker._diagnosticSourceType),
                                methodInfo,
                                exprEventNameParam,
                                exprArg1Param,
                                exprArg2Param),
                            exprDiagnosticSourceInstanceParam,
                            exprEventNameParam,
                            exprArg1Param,
                            exprArg2Param);

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.IsEnabled, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticSource),
                                                             $"Error while building the invocation delegate for the API \"{nameof(IsEnabled)}\".",
                                                             ex);
                    }
                }

                bool result = invokerDelegate(diagnosticSourceInstance, eventName, arg1, arg2);

                return(result);
            }
            public IDisposable Subscribe(object diagnosticListenerInstance,
                                         IObserver <KeyValuePair <string, object> > eventObserver,
                                         Func <string, object, object, bool> isEventEnabledFilter)
            {
                Func <object, IObserver <KeyValuePair <string, object> >, Func <string, object, object, bool>, IDisposable> invokerDelegate = _cashedDelegates.Subscribe;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = (diagnosticListenerInstance, eventObserver, isEventEnabledFilter) =>
                        //                              ((DiagnosticListener) diagnosticListenerInstance).Subscribe(eventObserver, isEventEnabledFilter);

                        ParameterExpression exprDiagnosticListenerInstanceParam = Expression.Parameter(typeof(object), "diagnosticListenerInstance");
                        ParameterExpression exprEventObserverParam        = Expression.Parameter(typeof(IObserver <KeyValuePair <string, object> >), "eventObserver");
                        ParameterExpression exprIsEventEnabledFilterParam = Expression.Parameter(typeof(Func <string, object, object, bool>), "isEventEnabledFilter");

                        MethodInfo methodInfo = _thisInvoker._diagnosticListenerType.GetMethod("Subscribe",
                                                                                               BindingFlags.Instance | BindingFlags.Public,
                                                                                               binder: null,
                                                                                               new Type[] { typeof(IObserver <KeyValuePair <string, object> >),
                                                                                                            typeof(Func <string, object, object, bool>) },
                                                                                               modifiers: null);

                        var exprInvoker = Expression.Lambda <Func <object, IObserver <KeyValuePair <string, object> >, Func <string, object, object, bool>, IDisposable> >(
                            Expression.Call(
                                Expression.Convert(exprDiagnosticListenerInstanceParam, _thisInvoker._diagnosticListenerType),
                                methodInfo,
                                exprEventObserverParam,
                                exprIsEventEnabledFilterParam),
                            exprDiagnosticListenerInstanceParam,
                            exprEventObserverParam,
                            exprIsEventEnabledFilterParam);

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.Subscribe, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticListener),
                                                             $"Error while building the invocation delegate for the API \"{nameof(Subscribe)}\".",
                                                             ex);
                    }
                }

                IDisposable result = invokerDelegate(diagnosticListenerInstance, eventObserver, isEventEnabledFilter);

                return(result);
            }
            public void Write(object diagnosticSourceInstance, string eventName, object payloadValue)
            {
                Action <object, string, object> invokerDelegate = _cashedDelegates.Write;

                if (invokerDelegate == null)
                {
                    try
                    {
                        // invokerDelegate = (diagnosticSourceInstance, eventName, payloadValue) =>
                        //                              ((Diagnosticource) diagnosticSourceInstance).Write(eventName, payloadValue);

                        ParameterExpression exprDiagnosticSourceInstanceParam = Expression.Parameter(typeof(object), "diagnosticSourceInstance");
                        ParameterExpression exprEventNameParam    = Expression.Parameter(typeof(string), "eventName");
                        ParameterExpression exprPayloadValueParam = Expression.Parameter(typeof(object), "payloadValue");

                        MethodInfo methodInfo = _thisInvoker._diagnosticSourceType.GetMethod("Write",
                                                                                             BindingFlags.Instance | BindingFlags.Public,
                                                                                             binder: null,
                                                                                             new Type[] { typeof(string), typeof(object) },
                                                                                             modifiers: null);

                        var exprInvoker = Expression.Lambda <Action <object, string, object> >(
                            Expression.Call(
                                Expression.Convert(exprDiagnosticSourceInstanceParam, _thisInvoker._diagnosticSourceType),
                                methodInfo,
                                exprEventNameParam,
                                exprPayloadValueParam),
                            exprDiagnosticSourceInstanceParam,
                            exprEventNameParam,
                            exprPayloadValueParam);

                        invokerDelegate = exprInvoker.Compile();
                        invokerDelegate = Concurrent.TrySetOrGetValue(ref _cashedDelegates.Write, invokerDelegate);
                    }
                    catch (Exception ex)
                    {
                        throw new DynamicInvocationException(typeof(DynamicInvoker_DiagnosticSource),
                                                             $"Error while building the invocation delegate for the API \"{nameof(Write)}\".",
                                                             ex);
                    }
                }

                invokerDelegate(diagnosticSourceInstance, eventName, payloadValue);
            }