Пример #1
0
            public void addEventListener(String Event, object function)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("addEventListener(Event = {0}, Function = {1})", Event, function.ToString()));

                if (EventListeners.ContainsKey(Event))
                {
                    EventListeners.Remove(Event);
                }
                EventListeners.Add(Event, function);
            }
Пример #2
0
        public void Start(TflProcess process)
        {
            if (_started)
            {
                return;
            }

            _started = true;
            Name     = process.Name;
            foreach (var log in process.Log)
            {
                switch (log.Provider)
                {
                case "file":
                    log.Folder = log.Folder.Replace('/', '\\');
                    log.File   = log.File.Replace('/', '\\');
                    log.Folder = (log.Folder.Equals(Common.DefaultValue) ? "logs" : log.Folder).TrimEnd('\\') + "\\";
                    log.File   = (log.File.Equals(Common.DefaultValue) ? "tfl-" + process.Name + ".log" : log.File).TrimStart('\\');

                    var fileListener = new ObservableEventListener();
                    fileListener.EnableEvents(TflEventSource.Log, (EventLevel)Enum.Parse(typeof(EventLevel), log.Level));
                    SinkSubscriptions.Add(fileListener.LogToRollingFlatFile(log.Folder + log.File, 5000, "yyyy-MM-dd", RollFileExistsBehavior.Increment, RollInterval.Day, new LegacyLogFormatter(), 0, log.Async));
                    EventListeners.Add(fileListener);
                    break;

                case "mail":
                    if (log.Subject.Equals(Common.DefaultValue))
                    {
                        log.Subject = process.Name + " " + log.Level;
                    }
                    var mailListener = new ObservableEventListener();
                    mailListener.EnableEvents(TflEventSource.Log, EventLevel.Error);
                    SinkSubscriptions.Add(mailListener.LogToEmail(log));
                    EventListeners.Add(mailListener);
                    break;
                }
            }
        }
        /// <summary>
        /// Register a listener for a specific event with topic expressions
        /// </summary>
        protected virtual void RegisterEventListener(TypeInfo type, MethodInfo method)
        {
            Logger.LogDebug($"Checking if method {method.Name} is a suitable event listener");

            if (!IsMethodSuitableEvent(method, false))
            {
                Logger.LogCritical($"Method {method.Name} does not have a proper event listener signature in type {type.Name}");
                throw new BusConfigurationException($"Method {method.Name} does not have a proper eventlistener signature in type {type.Name}");
            }

            Logger.LogDebug($"Evaluating parameter type {type.Name} of method {method.Name}");
            Type parameterType = method.GetParameters().FirstOrDefault()?.ParameterType;

            TopicAttribute[] topicAttributes = method.GetCustomAttributes <TopicAttribute>()
                                               .ToArray();

            string[] topicPatterns = topicAttributes
                                     .Select(e => e.TopicPattern)
                                     .ToArray();

            Regex[] topicRegularExpressions = topicAttributes
                                              .Select(e => e.TopicRegularExpression)
                                              .ToArray();

            Logger.LogDebug($"Found topic patterns {string.Join(", ", topicPatterns)} on method {method.Name} in type {type.Name}");

            Logger.LogTrace($"Adding MicroserviceListener with type {type.Name} and method {method.Name}");
            EventListeners.Add(new MicroserviceListener
            {
                TopicExpressions        = topicPatterns,
                TopicRegularExpressions = topicRegularExpressions,
                Callback = message =>
                {
                    Logger.LogDebug($"Attempting to instantiate type {type.Name}");
                    object instance = InstantiatePopulatedType(type);

                    Logger.LogTrace($"Retrieving string data from message with id {message.CorrelationId}");
                    string text = Encoding.Unicode.GetString(message.Body);

                    Logger.LogTrace("Checking if parameter type is equal to a string");
                    if (parameterType == StringType)
                    {
                        Logger.LogTrace($"Parameter type is a string, invoking method for message {message.CorrelationId} with body {text}");
                        method.Invoke(instance, new object[] { text });
                        return;
                    }

                    try
                    {
                        Logger.LogTrace($"Deserialized object from message with id {message.CorrelationId} and body {text}");
                        object jsonObject = JsonConvert.DeserializeObject(text, parameterType);

                        Logger.LogTrace($"Invoking method {method.Name} with message id {message.CorrelationId} and instance " +
                                        $"of supposed type {type.Name} and actual type {jsonObject?.GetType().Name} with data {text}");

                        method.Invoke(instance, new[] { jsonObject });
                    }
                    catch (JsonReaderException readerException)
                    {
                        Logger.LogCritical($"JsonReaderException occured while deserializing message with type {message.EventType} and topic {message.Topic}," +
                                           $" consider changing the parameter type of method {method.Name} of type {type.Name} to string. Data: {text}, exception: {readerException.Message}");
                        throw;
                    }
                }
            });
        }