Example #1
0
        /// <summary>
        /// Returns the <see cref="IEntryWriter{TEntry}" /> matching with the specified <see cref="ILogWriterConfig" />.
        /// </summary>
        /// <typeparam name="TEntry">The logentry type written by the returned <see cref="IEntryWriter{TEntry}" />.</typeparam>
        /// <param name="logWriterConfig">An <see cref="ILogWriterConfig" /> instance.</param>
        /// <returns>
        /// An <see cref="IEntryWriter{TEntry}" /> for <paramref name="logWriterConfig" /> and of entry type
        /// <typeparamref name="TEntry" />.
        /// If the <c>logWriterConfig</c> is valid, but the log writer failed to start or doesn't contain an entry writer of the
        /// specified type,
        /// a <see cref="NoOpEntryWriter{TEntry}" /> is returned.
        /// </returns>
        /// <exception cref="KeyNotFoundException">
        /// If no value in <c>Config.Writers</c> is equal to
        /// <paramref name="logWriterConfig" />
        /// </exception>
        /// <remarks>
        /// This method throws exceptions if the call is invalid, but
        /// does not throw an exception if the returned logwriter failed to start.
        /// </remarks>
        public IEntryWriter <TEntry> GetEntryWriter <TEntry>(ILogWriterConfig logWriterConfig) where TEntry : ILogEntry
        {
            Contract.Requires <ArgumentNullException>(logWriterConfig != null);

            ILogWriter logWriter = null;

            logWriter = GetLogWriter(logWriterConfig);
            if (logWriter == null)
            { // This occurs when entryWriter.Start() fails.  In this case, the desired behavior is to return a functioning logwriter.
                var tracer = SetupTracerFactory.TracerFor(this);
                tracer.Warn("Returning a NoOpEntryWriter<{0}> for log writer config: {1} - check start errors.", typeof(TEntry).Name, logWriterConfig);
                return(new NoOpEntryWriter <TEntry>());
            }

            IEntryWriter <TEntry> entryWriter;

            if (!logWriter.TryGetEntryWriter(out entryWriter))
            {
                var tracer = SetupTracerFactory.TracerFor(this);
                tracer.Warn("Returning a NoOpEntryWriter<{0}> for log writer {1} - log writer did not contain an entry writer for log entry type {0}.",
                            typeof(TEntry).Name,
                            logWriterConfig);
                return(new NoOpEntryWriter <TEntry>());
            }
            return(entryWriter);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                Stop();

                // Protect against recursion (eg linked disposables could create a cycle)
                _startableState = StartableState.Disposed;

                if (disposing)
                {
                    // Dispose all the linked disposables
                    foreach (var disposable in _linkedDisposables)
                    {
                        if (disposable != null)
                        {
                            try
                            {
                                disposable.Dispose();
                            }
                            catch (ObjectDisposedException)
                            {} // No need to log this
                            catch (Exception exception)
                            {
                                var tracer = SetupTracerFactory.TracerFor(this);
                                tracer.Error(exception, "Exception while disposing " + disposable + ".");
                            }
                        }
                    }
                    _linkedDisposables.Clear();
                }
            }
        }
Example #3
0
        /// <summary>
        /// Create an <see cref="ILogWriter" /> from <paramref name="logWriterConfig" />, then proxy it as configured and log any
        /// errors in <see cref="SetupLog" />.
        /// </summary>
        /// <param name="logWriterConfig"></param>
        /// <returns></returns>
        internal ILogWriter CreateLogWriter(ILogWriterConfig logWriterConfig)
        {
            ILogWriter logWriter = null;

            try
            {
                logWriter = logWriterConfig.CreateLogWriter(SetupTracerFactory);

                if (logWriterConfig.BackgroundLogging)
                {
                    var backgroundWriter = new BackgroundMultiLogWriter(SetupTracerFactory);
                    _backgroundMultiLogWriters.Add(backgroundWriter);
                    logWriter = backgroundWriter.CreateProxyFor(logWriter);
                    backgroundWriter.Start();
                }
                else if (!logWriter.IsSynchronized && logWriterConfig.Synchronized)
                {
                    // Wrap non-synchronized LogWriters to make them threadsafe
                    logWriter = new SynchronizingProxyLogWriter(SetupTracerFactory, logWriter);
                }
            }
            catch (Exception excp)
            {
                // TODO: Store initialization failure status
                var tracer = SetupTracerFactory.TracerFor(logWriterConfig);
                tracer.Severe(excp, "Exception creating logwriter from config: {0}", logWriterConfig);
                logWriter = null;
            }

            return(logWriter);
        }
Example #4
0
        protected override void InternalStart()
        {
            lock (this)
            {
                if (_logManager.IsRestartNeeded())
                {
                    // Restart the LogManager to pick up any config changes
                    _logManager.Start();
                }
                else
                {
                    // No config changes needed, just start it if not already started
                    _logManager.EnsureAutoStarted();
                }

                // Create all the TraceWriters associated with each config entry
                _activeTraceEntryWriters.Clear();
                foreach (TraceWriterConfig traceWriterConfig in Config.Writers)
                {
                    try
                    {
                        IEntryWriter <TraceEntry> traceEntryWriter = LogManager.GetEntryWriter <TraceEntry>(traceWriterConfig.LogWriterConfig);
                        _activeTraceEntryWriters.Add(new Tuple <TraceWriterConfig, IEntryWriter <TraceEntry> >(traceWriterConfig, traceEntryWriter));
                    }
                    catch (Exception excp)
                    {
                        SetupTracerFactory.TracerFor(this)
                        .Error(excp, "Couldn't setup Tracing to target {0} due to exception getting IEntryWriter<TraceEntry>.", traceWriterConfig.LogWriterConfig);
                    }
                }

                // Reset TraceWriter for each Tracer
                ForEachTracer(tracer => tracer.Configure(GetTraceWritersFor(tracer.Name)));
            }
        }
        /// <summary>
        /// Stops this instance; closes all disposables.
        /// </summary>
        public void Stop()
        {
            if (IsStopped || (_startableState == StartableState.Unstarted) || (_startableState == StartableState.Stopping))
            {
                return;
            }

            var    tracer    = SetupTracerFactory.TracerFor(this);
            string className = GetType().Name;

            tracer.Info("Stopping " + className + "...");

            try
            {
                _startableState = StartableState.Stopping;
                InternalStop();
                _startableState = StartableState.Stopped;
            }
            catch (Exception stopException)
            {
                _startException = stopException;
                tracer.Error(stopException, "Stop failed: Exception occurred.");
                _startableState = StartableState.FailedToStop;
            }

            foreach (var disposableRef in _disposeOnStop)
            {
                IDisposable disposable = disposableRef.Target as IDisposable;
                if (disposable != null)
                {
                    try
                    {
                        disposable.Dispose();
                    }
                    catch (ObjectDisposedException)
                    {} // No need to log this
                    catch (Exception exception)
                    {
                        tracer.Error(exception, "Exception while disposing " + disposable + ".");
                    }
                }
            }
            _disposeOnStop.Clear();

            tracer.Info(className + " stopped.");
        }
Example #6
0
        protected override void InternalStart()
        {
            lock (this)
            {
                var logManagerTracer = SetupTracerFactory.TracerFor(this);

                if (_logWriters.Count > 0)
                {
                    logManagerTracer.Debug("Stopping LogManager before re-starting it...");
                    Stop();
                }

                foreach (ILogWriterConfig logWriterConfig in Config.Writers)
                {
                    if (_logWriters.ContainsKey(logWriterConfig))
                    {
                        // Occurs when the logWriterConfig already exists - this shouldn't happen
                        var tracer = SetupTracerFactory.TracerFor(logWriterConfig);
                        tracer.Severe("LogWriterConfig {0} is already active - this shouldn't happen.  Skipping it...", logWriterConfig);
                        continue;
                    }

                    ILogWriter logWriter = CreateLogWriter(logWriterConfig);

                    if (logWriter != null)
                    {
                        (logWriter as IStartable).SafeStart(SetupTracerFactory);

                        _logWriters.Add(logWriterConfig, logWriter);

                        if (logWriterConfig.DisposeOnStop)
                        {
                            DisposeOnStop(logWriter);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Resets this instance, to start new as an unconfigured instance with no memory of the past.
        /// </summary>
        /// <param name="clearSetupLog">If <c>true</c>, the contents of the <see cref="SetupTracerFactory" /> are cleared.</param>
        public void Reset(bool clearSetupLog)
        {
            lock (this)
            {
                var tracer = SetupTracerFactory.TracerFor(this);
                tracer.Info("Resetting... ");

                Stop();

                if (clearSetupLog)
                {
                    var setupTracerFactory = SetupTracerFactory as SetupLog;
                    if (setupTracerFactory != null)
                    {
                        setupTracerFactory.Clear();
                    }
                }

                InternalReset();

                tracer.Info("Completed Reset. ");
            }
        }
        /// <summary>
        /// Starts the manager whether or not <c>Start()</c> has already been called.
        /// </summary>
        /// <remarks>To avoid starting more than once, use <see cref="EnsureAutoStarted" />.</remarks>
        public void Start()
        {
            var    tracer    = SetupTracerFactory.TracerFor(this);
            string className = GetType().Name;

            if (IsDisposed)
            {
                tracer.Error(className + " cannot be started; it has been Dispose()ed.");
                return;
            }
            var state = _startableState;

            if ((state == StartableState.Starting) || (state == StartableState.Stopping))
            {
                tracer.Error(className + " cannot be started; state is: " + state);
                return;
            }

            tracer.Debug("Starting " + className + "...");

            lock (this)
            {
                try
                {
                    _startableState = StartableState.Starting;
                    InternalStart();
                    _startableState = StartableState.Started;
                    tracer.Info(className + " started.");
                }
                catch (Exception startException)
                {
                    _startException = startException;
                    tracer.Error(startException, "Start failed: Exception occurred.");
                    _startableState = StartableState.FailedToStart;
                }
            }
        }