/// <summary>
        ///     The publish.
        /// </summary>
        protected virtual void Publish()
        {
            var task = new Task(
                () =>
            {
                var itemsToLog = new List <T>();
                try
                {
                    if (IsPublishing())
                    {
                        return;
                    }

                    StartPublishing();
                    //Debug.WriteLine($"Log start dequeue {this.Count} items.");
                    T item;
                    while (TryDequeue(out item))
                    {
                        itemsToLog.Add(item);
                    }
                }
                catch (ThreadAbortException tex)
                {
                    Debug.WriteLine($"Warning-Dequeue items failed > {tex.Message}");

                    LogEngine.WriteLog(
                        ConfigurationBag.EngineName,
                        $"Error in {MethodBase.GetCurrentMethod().Name}",
                        Constant.LogLevelError,
                        Constant.TaskCategoriesError,
                        tex,
                        Constant.LogLevelError);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Dequeue items failed > {ex.Message}");

                    LogEngine.WriteLog(
                        ConfigurationBag.EngineName,
                        $"Error in {MethodBase.GetCurrentMethod().Name}",
                        Constant.LogLevelError,
                        Constant.TaskCategoriesError,
                        ex,
                        Constant.LogLevelError);
                }
                finally
                {
                    //Debug.WriteLine($"Log dequeued {itemsToLog.Count} items");
                    OnPublish(itemsToLog);
                    CompletePublishing();
                }
            });

            task.Start();
        }
Exemple #2
0
        public static void Init()
        {
            try
            {
                Configuration.LoadConfiguration();
                Enabled = Configuration.LoggingEngineEnabled();
                Verbose = Configuration.LoggingVerbose();
                //Load logging external component
                var loggingComponent = Path.Combine(
                    Configuration.DirectoryOperativeRootExeName(),
                    Configuration.LoggingComponent());

                DirectConsoleWriteLine("Check Abstract Logging Engine.", ConsoleColor.Yellow);

                //Create the reflection method cached
                var assembly = Assembly.LoadFrom(loggingComponent);
                //Main class logging
                var assemblyClass = (from t in assembly.GetTypes()
                                     let attributes = t.GetCustomAttributes(typeof(LogContract), true)
                                                      where t.IsClass && attributes != null && attributes.Length > 0
                                                      select t).First();

                var classAttributes = assemblyClass.GetCustomAttributes(typeof(LogContract), true);

                if (classAttributes.Length > 0)
                {
                    Debug.WriteLine("LogEventUpStream - InitLog caller");
                    methodLogInfoInit = assemblyClass.GetMethod("InitLog");
                    Debug.WriteLine("LogEventUpStream - WriteLog caller");
                    methodLogInfoWrite = assemblyClass.GetMethod("WriteLog");
                }

                classInstance = Activator.CreateInstance(assemblyClass, null);

                Debug.WriteLine("LogEventUpStream - Inizialize the external log");
                methodLogInfoInit.Invoke(classInstance, null);
                DirectConsoleWriteLine("Initialize Abstract Logging Engine.", ConsoleColor.Yellow);

                Debug.WriteLine("LogEventUpStream - CreateEventSource if not exist");
                if (!EventLog.SourceExists(EventViewerSource))
                {
                    EventLog.CreateEventSource(EventViewerSource, EventViewerLog);
                }

                //Create the QueueConsoleMessage internal queue
                Debug.WriteLine(
                    "LogEventUpStream - logQueueConsoleMessage.OnPublish += LogQueueConsoleMessageOnPublish");
                QueueConsoleMessage =
                    new LogQueueConsoleMessage(
                        Configuration.ThrottlingConsoleLogIncomingRateNumber(),
                        Configuration.ThrottlingConsoleLogIncomingRateSeconds());
                QueueConsoleMessage.OnPublish += QueueConsoleMessageOnPublish;

                //Create the QueueAbstractMessage internal queue
                Debug.WriteLine(
                    "LogEventUpStream - logQueueAbstractMessage.OnPublish += LogQueueAbstractMessageOnPublish");
                QueueAbstractMessage = new LogQueueAbstractMessage(
                    Configuration.ThrottlingLsiLogIncomingRateNumber(),
                    Configuration.ThrottlingLsiLogIncomingRateSeconds());
                QueueAbstractMessage.OnPublish += QueueAbstractMessageOnPublish;
                Debug.WriteLine("LogEventUpStream - Log Queues initialized.");
            }
            catch (Exception ex)
            {
                LogEngine.EventViewerWriteLog(
                    Configuration.EngineName,
                    $"Error in {MethodBase.GetCurrentMethod().Name}",
                    Constant.DefconOne,
                    Constant.TaskCategoriesError,
                    ex,
                    EventLogEntryType.Error);
                LogEngine.WriteLog(
                    Configuration.EngineName,
                    $"Error in {MethodBase.GetCurrentMethod().Name}",
                    Constant.DefconOne,
                    Constant.TaskCategoriesError,
                    ex,
                    EventLogEntryType.Error);
                Thread.Sleep(Configuration.WaitTimeBeforeRestarting());
                Environment.Exit(0);
            }
        }