示例#1
0
        public static void Main(string[] args)
        {
            Configuration       configuration     = new Configuration();
            WcfLoggerConfigView configurationView = new WcfLoggerConfigView();

            configuration.RegisterConfigurationView(configurationView);
            configuration.RegisterConfigurationSource(new AppSettingsConfigurationSource());

            Binding binding     = Program.ParseBinding(configurationView.Binding);
            Uri     uri         = new Uri(configurationView.Uri);
            string  serviceName = configurationView.ServiceName;

            var endpoints = new Dictionary <string, Binding>
            {
                { serviceName, binding }
            };

            using (LogService loggerService = new LogService())
            {
                ConsoleLogger consoleLogger = new ConsoleLogger();
                loggerService.RegisterInternalEndpoint(consoleLogger);
                loggerService.RegisterInternalEndpoint(
                    new TextLogger(logFileDirectory: string.Empty, logFilePrefix: "Live Viewer Errors - "));

                loggerService.RegisterEndpoint(consoleLogger);
                loggerService.RegisterEndpoint(
                    new TextLogger(
                        logFileDirectory: configurationView.LogFileDirectory,
                        logFilePrefix: configurationView.LogFilePrefix));

                loggerService.Log(LogLevel.Status, Program.LogInternalCategory, "The logger service is ready.");

                // Run the wcf service and wait for completion.
                try
                {
                    using (
                        WcfLogService.ServiceHolder wcfServiceHolder = WcfLogService.Run(
                            loggerService,
                            new[] { uri },
                            endpoints))
                    {
                        wcfServiceHolder.Wait(TimeSpan.FromMilliseconds(-1));
                    }
                }
                catch (Exception e)
                {
                    loggerService.LogFatal(Program.LogInternalCategory, $"An error has occurred: {e}");
                }
            }

            Console.WriteLine("Service has shut down. Press enter to exit.");
            Console.ReadLine();
        }
示例#2
0
        public void Wcf_Logging_Test()
        {
            const int numMessagesToLog = 21;

            var baseAddresses = new[] { DefaultWcfEndpoints.GetNetNamedPipeUri() };

            DefaultWcfEndpoints.GetHttpEndpointAddress();
            DefaultWcfEndpoints.GetNetTcpEndpointAddress();
            DefaultWcfEndpoints.GetMsmqEndpointAddress();

            TestEndpoint  internalEndpoint = new TestEndpoint();
            ConsoleLogger consoleLogger    = new ConsoleLogger();
            TestEndpoint  destEndpoint     = new TestEndpoint();
            TestEndpoint  sourceEndpoint   = new TestEndpoint();

            using (LogService destLogger = new LogService())
                using (LogService sourceLogger = new LogService())
                {
                    destLogger.RegisterInternalEndpoint(internalEndpoint);
                    destLogger.RegisterEndpoint(consoleLogger);
                    destLogger.RegisterEndpoint(destEndpoint);

                    using (WcfLogService.ServiceHolder serviceHolder = WcfLogService.Run(
                               destLogger,
                               baseAddresses,
                               DefaultWcfEndpoints.DefaultNamedPipeServiceName,
                               new NetNamedPipeBinding()))
                    {
                        Assert.IsTrue(serviceHolder.IsRunning);

                        // Wait for the WCF service to spin up.
                        while (internalEndpoint.NumMessagesLogged == 0)
                        {
                            Thread.Sleep(16);
                        }

                        WcfLoggerConfigView      configView   = new WcfLoggerConfigView();
                        DebugConfigurationSource configSource = new DebugConfigurationSource();
                        Configuration            config       = new Configuration();
                        config.RegisterConfigurationSource(configSource);
                        config.RegisterConfigurationView(configView);

                        configSource.TryAddOrUpdateSetting("binding", "netHttp");
                        configSource.TryAddOrUpdateSetting("logfiledirectory", "dir");
                        configSource.TryAddOrUpdateSetting("logfileprefix", "prefix");
                        configSource.TryAddOrUpdateSetting("servicename", "service");
                        configSource.TryAddOrUpdateSetting("uri", "uri");
                        using (new WcfLogger(
                                   destLogger,
                                   new BasicHttpBinding(),
                                   new EndpointAddress("http://FakeEndpoint")))
                        {
                        }

                        using (WcfLogger wcfLogger = new WcfLogger(
                                   destLogger,
                                   new NetNamedPipeBinding(),
                                   DefaultWcfEndpoints.GetNetNamedPipeEndpointAddress(),
                                   10,
                                   TimeSpan.FromMilliseconds(250)))
                        {
                            sourceLogger.RegisterEndpoint(consoleLogger);
                            sourceLogger.RegisterEndpoint(sourceEndpoint);
                            sourceLogger.RegisterEndpoint(wcfLogger);

                            Random r = new Random();
                            for (int i = 0; i < numMessagesToLog; i++)
                            {
                                sourceLogger.LogInfo($"Test message {i}");
                                Thread.Sleep(r.Next(50));
                            }

                            while (destEndpoint.NumMessagesLogged < numMessagesToLog)
                            {
                            }

                            sourceLogger.SignalShutDown(TimeSpan.FromSeconds(5));
                            destLogger.SignalShutDown(TimeSpan.FromSeconds(5));
                        }

                        serviceHolder.Shutdown(TimeSpan.FromMilliseconds(-1));
                    }
                }

            Assert.AreEqual(numMessagesToLog, destEndpoint.NumMessagesLogged);
        }