<commit_before>﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks;
using UAOOI.Networking.ReferenceApplication.Consumer;
using UAOOI.Networking.SemanticData.Diagnostics;

namespace UAOOI.Networking.ReferenceApplication.MEF
{
  [Export(typeof(EventSourceBootstrapper))]
  public class EventSourceBootstrapper : IDisposable
  {

    #region composition
    [ImportMany(typeof(INetworkingEventSourceProvider))]
    public IEnumerable<INetworkingEventSourceProvider> EventSources { get; set; }
    [Import(typeof(IConsumerViewModel))]
    internal IConsumerViewModel ViewModel { get; set; }
    #endregion

    internal void Run()
    {
      CompositeDisposable _listenersDisposable = new CompositeDisposable();
      if (EventSources != null)
        foreach (INetworkingEventSourceProvider _eventSources in EventSources)
        {
          ObservableEventListener _newEventListener = new ObservableEventListener();
          _newEventListener.EnableEvents(_eventSources.GetPartEventSource(), EventLevel.LogAlways, Keywords.All);
          _listenersDisposable.Add(_newEventListener);
        }
      if (_listenersDisposable.Count == 0)
        return;
      IObservable<EventEntry> _last = _listenersDisposable.Cast<IObservable<EventEntry>>().Concat();
      m_FileSubscription = _last.LogToFlatFile(Properties.Settings.Default.LogFilePath);
      m_Subscription = new SinkSubscription<CompositeDisposable>(_last.Subscribe<EventEntry>(x => ViewModel.Trace(x.FormattedMessage)), _listenersDisposable);
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls
    protected virtual void Dispose(bool disposing)
    {
      if (!disposedValue)
      {
        if (disposing)
        {
          m_FileSubscription?.Sink.FlushAsync();
          m_FileSubscription?.Dispose();
          m_FileSubscription = null;
          m_Subscription?.Dispose();
          m_Subscription = null;
        }
        // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
        // TODO: set large fields to null.
        disposedValue = true;
      }
    }
    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~EventSourceBootsraper() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }
    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
      // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
      Dispose(true);
      // TODO: uncomment the following line if the finalizer is overridden above.
      // GC.SuppressFinalize(this);
    }
    #endregion

    #region private
    private SinkSubscription<CompositeDisposable> m_Subscription;
    private SinkSubscription<FlatFileSink> m_FileSubscription;
    #endregion

  }
  public static class SinkExtensions
  {

    public static SinkSubscription<ObservableEventListener> CreateSink(this ObservableEventListener eventStream, Action<EventEntry> feedback)
    {
      IDisposable subscription = eventStream.Subscribe(feedback);
      return new SinkSubscription<ObservableEventListener>(subscription, eventStream);
    }

    public static SinkSubscription<ObservableEventListener> CreateSink(Action<EventEntry> feedback)
    {
      ObservableEventListener _listener = new ObservableEventListener();
      IDisposable subscription = _listener.Subscribe(feedback);
      return new SinkSubscription<ObservableEventListener>(subscription, _listener);
    }
    public static SinkSubscription<FlatFileSink> CreateSink(EventSource eventSource, string path)
    {
      ObservableEventListener _listener = new ObservableEventListener();
      _listener.EnableEvents(eventSource, EventLevel.LogAlways, Keywords.All);
      return _listener.LogToFlatFile(path);
    }

  }
}<commit_msg>* Networking - add consistent tracing infrastructure #225 working on the bootstrapping sequence in context of logging, - fixed bootstrapping procedure - replaced reactive Concat to Merge the log works as expected.<commit_after>﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks;
using UAOOI.Networking.ReferenceApplication.Consumer;
using UAOOI.Networking.SemanticData.Diagnostics;

namespace UAOOI.Networking.ReferenceApplication.MEF
{
  [Export(typeof(EventSourceBootstrapper))]
  public class EventSourceBootstrapper : IDisposable
  {

    #region composition
    [ImportMany(typeof(INetworkingEventSourceProvider))]
    public IEnumerable<INetworkingEventSourceProvider> EventSources { get; set; }
    [Import(typeof(IConsumerViewModel))]
    internal IConsumerViewModel ViewModel { get; set; }
    #endregion

    internal void Run()
    {
      CompositeDisposable _listenersDisposable = new CompositeDisposable();
      if (EventSources != null)
        foreach (INetworkingEventSourceProvider _eventSources in EventSources)
        {
          ObservableEventListener _newEventListener = new ObservableEventListener();
          _newEventListener.EnableEvents(_eventSources.GetPartEventSource(), EventLevel.LogAlways, Keywords.All);
          _listenersDisposable.Add(_newEventListener);
        }
      if (_listenersDisposable.Count == 0)
        return;
      IObservable<EventEntry> _last = _listenersDisposable.Cast<IObservable<EventEntry>>().Merge<EventEntry>();
      m_FileSubscription = _last.LogToFlatFile(Properties.Settings.Default.LogFilePath);
      m_Subscription = new SinkSubscription<CompositeDisposable>(_last.Subscribe<EventEntry>(x => ViewModel.Trace(x.FormattedMessage)), _listenersDisposable);
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls
    protected virtual void Dispose(bool disposing)
    {
      if (!disposedValue)
      {
        if (disposing)
        {
          m_FileSubscription?.Sink.FlushAsync();
          m_FileSubscription?.Dispose();
          m_FileSubscription = null;
          m_Subscription?.Dispose();
          m_Subscription = null;
        }
        // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
        // TODO: set large fields to null.
        disposedValue = true;
      }
    }
    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~EventSourceBootsraper() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }
    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
      // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
      Dispose(true);
      // TODO: uncomment the following line if the finalizer is overridden above.
      // GC.SuppressFinalize(this);
    }
    #endregion

    #region private
    private SinkSubscription<CompositeDisposable> m_Subscription;
    private SinkSubscription<FlatFileSink> m_FileSubscription;
    #endregion

  }
  //public static class SinkExtensions
  //{

  //  public static SinkSubscription<ObservableEventListener> CreateSink(this ObservableEventListener eventStream, Action<EventEntry> feedback)
  //  {
  //    IDisposable subscription = eventStream.Subscribe(feedback);
  //    return new SinkSubscription<ObservableEventListener>(subscription, eventStream);
  //  }

  //  public static SinkSubscription<ObservableEventListener> CreateSink(Action<EventEntry> feedback)
  //  {
  //    ObservableEventListener _listener = new ObservableEventListener();
  //    IDisposable subscription = _listener.Subscribe(feedback);
  //    return new SinkSubscription<ObservableEventListener>(subscription, _listener);
  //  }
  //  public static SinkSubscription<FlatFileSink> CreateSink(EventSource eventSource, string path)
  //  {
  //    ObservableEventListener _listener = new ObservableEventListener();
  //    _listener.EnableEvents(eventSource, EventLevel.LogAlways, Keywords.All);
  //    return _listener.LogToFlatFile(path);
  //  }

  //}
}
{'fraction_non_alphanumeric': 0.08411431216012959, 'fraction_numerical': 0.0005785028346638898, 'mean_word_length': 3.796892341842397, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035169', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2316, 'n_words': 538}