Ejemplo n.º 1
0
        public static void Startup()
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         // The following requires https://getseq.net/
                         .WriteTo.Seq("http://localhost:5341/")
                         .ConfigureDefaultOperationsDestructuring()
                         .Enrich.FromLogContext()
                         .Enrich.With <HttpRequestIdEnricher>()
                         .Enrich.With <HttpRequestTraceIdEnricher>()
                         .Enrich.With <UserNameEnricher>()
                         .Enrich.FromLogContext()
                         .CreateLogger();

            SelfLog.Enable(msg =>
            {
                Debug.WriteLine(msg);
            });

            OperationsLog.Enable(msg => Log.ForContext <OperationsConfig>().Warning(msg));
            OperationsSerilogDefaults.Apply();
            Operations.Serilog.Factories.UseDestructuring();

            Op.Runner = Op.Configure(x => x
                                     .Track.With <StatusTracker>()
                                     .Track.With <ProfilingTracker>()
                                     .Track.With <SerilogOperationTracker>())
                        .CreateRunner();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.ColoredConsole(LogEventLevel.Debug, "{Level:u3} {Timestamp:yyyy-MM-dd HH:mm:ss} {Message}{NewLine}{Exception}{NewLine}{Properties}{NewLine}{NewLine}")
                         // The following requires https://getseq.net/
                         .ConfigureDefaultOperationsDestructuring()
                         .WriteTo.Seq("http://localhost:5341/")
                         .Enrich.FromLogContext()
                         .CreateLogger();

            SelfLog.Enable(msg =>
            {
                Console.WriteLine(msg);
                Debug.WriteLine(msg);
            });

            OperationsLog.Enable(Console.WriteLine);
            OperationsSerilogDefaults.Apply();

            Operations.Serilog.Factories.UseDestructuring();

            Op.Runner = Op.Configure(x => x
                                     .Track.With <StatusTracker>()
                                     .Track.With <ProfilingTracker>()
                                     .Track.With <SerilogOperationTracker>())
                        .CreateRunner();

            try
            {
                using (var op = Op.Start("root", Op.Context(new { Hello = "World" })))
                {
                    Thread.Sleep(1000);

                    for (int i = 1; i < 4; ++i)
                    {
                        var test = i;

                        op.Update($"something new happended: starting #{test}");
                        // todo: support Op.Run("name", op => { ... op.Update() } })

                        Op.Run($"child #{test}", () =>
                        {
                            /* and Run/RunAsync automatically track operation status
                             * and track operation errors (by calling Operation.Error) when exception occurs
                             */
                            Thread.Sleep(1000);

                            if (test == 3)
                            {
                                throw new Exception("space oddity");
                            }

                            Log.Logger.Information("hello from child");
                        }, Op.Context(new { parent = "root" }));
                    }

                    Log.Logger.Information("root: child is finished");
                    Thread.Sleep(1000);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
            finally
            {
                Console.WriteLine("Press any key...");
                Console.ReadKey();
            }
        }
Ejemplo n.º 3
0
        public virtual void Startup()
        {
            if (_configuration.ApplicationInsightsEnabled)
            {
                TelemetryConfiguration.Active.InstrumentationKey = _configuration.InstrumentationKey;

                if (_configuration.FilterTelemetry)
                {
                    var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
                    builder.Use(x => new FilterTelemetryProcessor(x));
                    builder.BuildAndReinitialize();
                }

                _telemetryClient = new TelemetryClient(TelemetryConfiguration.Active);
            }
            else
            {
                TelemetryConfiguration.Active.DisableTelemetry = true;
            }

            FilterTelemetryConfiguration.Getter = () => ServiceLocator.Current.GetInstance <IFilterTelemetryConfiguration>();
            OperationsSerilogDefaults.Apply();

            if (_configuration.LogActions)
            {
                GlobalFilters.Filters.Add(new OperationsActionFilter());
                GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new WebApiExceptionLogger());
            }

            var log = new LoggerConfiguration();

            if (_configuration.ApplicationInsightsEnabled)
            {
                log = log.WriteTo.ApplicationInsights(_telemetryClient, LogEventToTelemetryConverter);
            }

            if (_configuration.SeqEnabled)
            {
                log = log.WriteTo.Seq(_configuration.SeqUrl);
            }

            if (_configuration.DestructureContextData)
            {
                log.ConfigureDefaultOperationsDestructuring();
                Operations.Serilog.Factories.UseDestructuring();
            }

            log
            .MinimumLevel.Is(_configuration.LogLevel)
            // .WriteTo.RollingFile($"{logDir}EPiServer.log", shared: true)
            .WriteTo.Log4Net("episerver")
            .Enrich.WithExceptionDetails()
            .Enrich.FromLogContext()
            .Enrich.With <HttpRequestIdEnricher>()
            .Enrich.With <HttpRequestRawUrlEnricher>()
            .Enrich.With <HttpRequestUrlEnricher>()
            .Enrich.With <HttpRequestUrlReferrerEnricher>()
            .Enrich.With <HttpRequestUserAgentEnricher>()
            .Enrich.With <HttpRequestTraceIdEnricher>()
            .Enrich.With <UserNameEnricher>()
            .Enrich.With <MvcControllerNameEnricher>()
            .Enrich.With <MvcActionNameEnricher>()
            .Enrich.With <MvcRouteTemplateEnricher>()
            .Enrich.With <MvcRouteDataEnricher>()
            .Enrich.With <WebApiControllerNameEnricher>()
            .Enrich.With <WebApiActionNameEnricher>()
            .Enrich.With <WebApiRouteDataEnricher>()
            .Enrich.With <WebApiRouteTemplateEnricher>()
            .Enrich.With <EPiServerEditModeEnricher>();

            Log.Logger = log.CreateLogger();

            var now = DateTime.Now;

            EPiServer.Logging.LogManager.Instance.AddFactory(new SerilogFactory());

            var episerverLog = LogManager.GetLogger(typeof(AppLogger));

            if (_configuration.DebugLog)
            {
                Serilog.Debugging.SelfLog.Enable(LogLogMessage);

                Log.Logger.Verbose("Application started: {time}", now);
                Log.Logger.Debug("Application started: {time}", now);
                Log.Logger.Information("Application started: {time}", now);
                Log.Logger.Warning("Application started: {time}", now);
                Log.Logger.Error("Application started: {time}", now);
                Log.Logger.Fatal("Application started: {time}", now);

                episerverLog.Debug("EPISERVER Application started");
                episerverLog.Warn("EPISERVER Application started");
                episerverLog.Info("EPISERVER Application started");
                episerverLog.Error("EPISERVER Application started");
                episerverLog.Fatal("EPISERVER Application started");
            }
            else
            {
                Log.Logger.Information("Application started");
            }

            if (_configuration.DebugLog)
            {
                OperationsLog.Enable(msg => Log.ForContext <AppLogger>().Warning(msg));
            }
            else
            {
                OperationsLog.Disable();
            }

            Op.Runner = Op.Configure(x => x
                                     .Track.With <StatusTracker>()
                                     .Track.With <ProfilingTracker>()
                                     .Track.With <SerilogOperationTracker>()).CreateRunner();
        }