예제 #1
0
        /// <summary>
        /// Enables logging of all HTTP requests to the specified <paramref name="logWriter" />.
        /// </summary>
        /// <param name="appBuilder"></param>
        /// <param name="logWriter">Specifies the log writer to use for HTTP logging.</param>
        /// <remarks>This middleware should be enabled at or near the beginning of the OWIN pipeline.</remarks>
        public static void LogHttpRequests(this IAppBuilder appBuilder, ILogWriterConfig logWriter)
        {
            Contract.Requires <ArgumentNullException>(appBuilder != null);
            Contract.Requires <ArgumentNullException>(logWriter != null);

            LogHttpRequests(appBuilder, new[] { logWriter });
        }
예제 #2
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);
        }
예제 #3
0
        public TraceWriterConfig(ILogWriterConfig logWriterConfig, SwitchSet switches = null)
        {
            Contract.Requires <ArgumentNullException>(logWriterConfig != null);

            _tracelogWriterConfig = logWriterConfig;
            _switches             = switches ?? new SwitchSet();
        }
예제 #4
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);
        }
예제 #5
0
 /// <summary>
 /// Event handler called when <paramref name="logWriterConfig"/> is removed from the <c>LogManager.Config.Writers</c> collection.
 /// </summary>
 /// <param name="logWriterConfig"></param>
 private void OnRemovingLogWriterConfig(ILogWriterConfig logWriterConfig)
 {
     if (logWriterConfig != null)
     {
         var removeSet = Writers.Where(twc => twc.LogWriterConfig == logWriterConfig);
         Writers.ExceptWith(removeSet);
     }
 }
예제 #6
0
        /// <summary>
        /// Returns the <see cref="ILogWriter" /> created from the specified <paramref name="logWriterConfig" />.
        /// </summary>
        /// <param name="logWriterConfig"></param>
        /// <returns>The <see cref="ILogWriter" /> created from <paramref name="logWriterConfig" /> if one exists; otherwise null.</returns>
        /// <exception cref="KeyNotFoundException">
        /// If no value in <c>Config.Writers</c> is equal to
        /// <paramref name="logWriterConfig" />
        /// </exception>
        public ILogWriter GetLogWriter(ILogWriterConfig logWriterConfig)
        {
            Contract.Requires <ArgumentNullException>(logWriterConfig != null);

            ILogWriter logWriter = null;

            if (!TryGetLogWriter(logWriterConfig, out logWriter))
            {
                throw new KeyNotFoundException("LogManager does not contain logWriterConfig: " + logWriterConfig);
            }
            return(logWriter);
        }
예제 #7
0
        /// <summary>
        /// Returns the <see cref="ILogWriter" /> created from the specified <paramref name="logWriterConfig" />.  If this
        /// <c>LogManager</c> has not yet been started, it is started first.
        /// </summary>
        /// <param name="logWriterConfig"></param>
        /// <param name="logWriter">
        /// Returns the <see cref="ILogWriter" /> created from <paramref name="logWriterConfig" /> if one exists; otherwise
        /// <c>null</c>.
        /// </param>
        /// <returns><c>true</c> if a <paramref name="logWriter" /> was found; <c>false</c> if no match was found.</returns>
        public bool TryGetLogWriter(ILogWriterConfig logWriterConfig, out ILogWriter logWriter)
        {
            Contract.Requires <ArgumentNullException>(logWriterConfig != null);

            // Even if Start() wasn't 100% successful, we still return any logwriters that were successfully started.
            EnsureAutoStarted();

            lock (this)
            {
                return(_logWriters.TryGetValue(logWriterConfig, out logWriter));
            }
        }
예제 #8
0
        public void AddingTraceWriterConfigUpdatesLogManagerConfig()
        {
            var textWriterLogWriterConfig = new TextWriterLogWriterConfig(new StringWriter());

            var logWriterConfigs = new ILogWriterConfig[]
            {
                new ListLogWriterConfig <TraceEntry>(),
                textWriterLogWriterConfig
            };

            using (var traceManager = new TraceManager())
            {
                foreach (var logWriterConfig in logWriterConfigs)
                {
                    traceManager.Config.Writers.Add(new TraceWriterConfig(logWriterConfig, TraceManagerConfig.CreateDefaultSwitchSet()));

                    Assert.Contains(traceManager.LogManager.Config.Writers, lwc => lwc == logWriterConfig);
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Creates a new <see cref="TraceManager" /> configured to use <paramref name="logWriterConfig" /> and
        /// <paramref name="traceSwitch" /> for all <see cref="Tracer" />s.
        /// </summary>
        /// <param name="logWriterConfig">The <see cref="ILogWriterConfig" /> to use to configure tracing.</param>
        /// <param name="traceSwitch">
        /// A <see cref="ITraceSwitch" /> to use for all <see cref="Tracer" />s.  If
        /// <c>null</c>, all <see cref="Tracer" /> calls of severity <see cref="TraceLevel.Info" /> or higher are written.
        /// </param>
        /// <param name="tracerNamePrefix">
        /// The <see cref="Tracer.Name" /> prefix to use.  Tracing will not occur if the
        /// <c>Tracer.Name</c> doesn't match this prefix.  By default, <see cref="Tracer.All" /> is used.
        /// </param>
        public TraceManager(ILogWriterConfig logWriterConfig, ITraceSwitch traceSwitch = null, string tracerNamePrefix = Tracer.All)
        {
            Contract.Requires <ArgumentNullException>(logWriterConfig != null);

            if (traceSwitch == null)
            {
                traceSwitch = TraceManagerConfig.CreateDefaultTraceSwitch();
            }

            // REVIEW: The need for this is messy, and we miss cases (eg multiple existing logwriters used) - perhaps each component manages its own messages?
            // If there's an existing LogWriter in the logwriter config, use its SetupLog, if any.
            ITracerFactory setupTracerFactory         = null;
            var            useExistingLogWriterConfig = logWriterConfig as UseExistingLogWriterConfig;

            if (useExistingLogWriterConfig != null)
            {
                var component = useExistingLogWriterConfig.LogWriter as ILogJamComponent;
                if (component != null)
                {
                    setupTracerFactory = component.SetupTracerFactory;
                }
            }

            _logManager = new LogManager(new LogManagerConfig(), setupTracerFactory);
            LinkDispose(_logManager); // b/c the LogManager is owned by this
            _traceConfig = new TraceManagerConfig(_logManager.Config)
            {
                Writers =
                {
                    new TraceWriterConfig(logWriterConfig)
                    {
                        Switches =
                        {
                            { tracerNamePrefix, traceSwitch }
                        }
                    }
                }
            };
        }
예제 #10
0
        public void RemovingTraceWriterConfigDoesNotRemoveLogWriterConfigs()
        {
            var textWriterLogWriterConfig = new TextWriterLogWriterConfig(new StringWriter());

            var logWriterConfigs = new ILogWriterConfig[]
            {
                new ListLogWriterConfig <TraceEntry>(),
                textWriterLogWriterConfig
            };
            var traceWriterConfigs = new List <TraceWriterConfig>();

            using (var traceManager = new TraceManager())
            {
                foreach (var logWriterConfig in logWriterConfigs)
                {
                    traceWriterConfigs.Add(new TraceWriterConfig(logWriterConfig, TraceManagerConfig.CreateDefaultSwitchSet()));
                }
                traceManager.Config.Writers.UnionWith(traceWriterConfigs);

                // Test removing each
                for (int i = 0; i < logWriterConfigs.Length; ++i)
                {
                    var logWriterConfig   = logWriterConfigs[i];
                    var traceWriterConfig = traceWriterConfigs[i];

                    // Each logWriterConfig should exist in the LogManagerConfig before it is removed from the TraceManagerConfig
                    Assert.Contains(traceManager.LogManager.Config.Writers, lwc => lwc == logWriterConfig);

                    traceManager.Config.Writers.Remove(traceWriterConfig);
                    Assert.DoesNotContain(traceManager.Config.Writers, twc => twc == traceWriterConfig);

                    // LogWriters are left in place, because they may be used for other purposes
                    Assert.Contains(traceManager.LogManager.Config.Writers, lwc => lwc == logWriterConfig);
                }
            }
        }