Beispiel #1
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();
        }