static void CheckProperties(Func <LogEvent> logEventProvider, ElasticsearchJsonFormatter formatter, Action <string> assert) { string result = null; var logEvent = logEventProvider(); using (var stringWriter = new StringWriter()) { formatter.Format(logEvent, stringWriter); result = stringWriter.ToString(); } assert(result); }
public static Serilog.ILogger AddCustomLogging(IServiceCollection services, IConfiguration configuration, string serviceName) { var logFormatter = new ElasticsearchJsonFormatter(); var rabbitCliente = new RabbitMQClientConfiguration(); configuration.Bind("rabbitLog", rabbitCliente); rabbitCliente.SslOption = new RabbitMQ.Client.SslOption { Enabled = false, }; rabbitCliente.DeliveryMode = RabbitMQDeliveryMode.Durable; var rabbitConfiguration = new RabbitMQSinkConfiguration { Period = TimeSpan.FromDays(1), TextFormatter = logFormatter, }; Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.WithProperty("App Name", serviceName) .Enrich.FromLogContext() .Enrich.WithEnvironmentName() .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.WithProperty("AuditLog")) .WriteTo.Console(logFormatter) .WriteTo.RabbitMQ(rabbitCliente, rabbitConfiguration, logFormatter) ) .CreateLogger(); var loggerFactory = new LoggerFactory(); loggerFactory .AddSerilog(); services.AddSingleton <ILoggerFactory>(loggerFactory); return(Log.Logger); }
public Startup(IConfiguration configuration) { var udpFormatter = new ElasticsearchJsonFormatter(); var loggerConfig = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .Enrich.FromLogContext() .MinimumLevel.Information() .Enrich.WithProperty("applicationName", Assembly.GetExecutingAssembly().GetName().Name) .Enrich.WithProperty("applicationVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString()); loggerConfig .WriteTo.Udp("127.0.0.1", 5044, AddressFamily.InterNetwork, udpFormatter); var logger = loggerConfig.CreateLogger(); Log.Logger = logger; Serilog.Debugging.SelfLog.Enable(Console.Error); Configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json") .Build(); }
static void CheckProperties(ElasticsearchJsonFormatter formatter, Action <string> assert) => CheckProperties(CreateLogEvent, formatter, assert);
protected void Application_Start() { //Init logger //Analytics logger if (new[] { SimpleSettings.EmailPassword, SimpleSettings.EmailServer, SimpleSettings.EmailUserName, SimpleSettings.FromEmail, SimpleSettings.ToEmails }.All(s => !string.IsNullOrEmpty(s))) { var customFormatter = new ElasticsearchJsonFormatter( renderMessage: false, closingDelimiter: string.Empty ); var customDurableFormatter = new ElasticsearchJsonFormatter( renderMessage: false, closingDelimiter: Environment.NewLine ); var elasticSearchConfig = new ElasticsearchSinkOptions(new Uri(SimpleSettings.ElasticSearchUri)) { AutoRegisterTemplate = true, CustomDurableFormatter = customDurableFormatter, CustomFormatter = customFormatter }; var analyticsLogger = new LoggerConfiguration() .Enrich.With(new ExperimentEnricher()) .Enrich.With(new UserNameEnricher()) .Destructure.JsonNetTypes() .WriteTo.Elasticsearch(elasticSearchConfig) .CreateLogger(); SimpleTrace.Analytics = analyticsLogger; //Diagnostics Logger var diagnosticsLogger = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.With(new ExperimentEnricher()) .Enrich.With(new UserNameEnricher()) .WriteTo.Elasticsearch(elasticSearchConfig) .WriteTo.File(@"D:\home\site\log.log") .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(Matching.WithProperty <int>("Count", p => p % 10 == 0)) .WriteTo.Email(new EmailConnectionInfo { EmailSubject = "TryAppService Alert", EnableSsl = true, FromEmail = SimpleSettings.FromEmail, MailServer = SimpleSettings.EmailServer, NetworkCredentials = new NetworkCredential(SimpleSettings.EmailUserName, SimpleSettings.EmailPassword), Port = 587, ToEmail = SimpleSettings.ToEmails }, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Fatal)) .CreateLogger(); SimpleTrace.Diagnostics = diagnosticsLogger; } else { var logger = new LoggerConfiguration().CreateLogger(); SimpleTrace.Diagnostics = logger; SimpleTrace.Analytics = logger; } SimpleTrace.Diagnostics.Information("Application started"); //Configure Json formatter GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter()); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Error = (sender, args) => { SimpleTrace.Diagnostics.Error(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; }; //Templates Routes RouteTable.Routes.MapHttpRoute("templates", "api/templates", new { controller = "Templates", action = "Get", authenticated = false }); //Telemetry Routes RouteTable.Routes.MapHttpRoute("post-telemetry-event", "api/telemetry/{telemetryEvent}", new { controller = "Telemetry", action = "LogEvent", authenticated = false }, new { verb = new HttpMethodConstraint("POST") }); RouteTable.Routes.MapHttpRoute("post-feedback-comment", "api/feedback", new { controller = "Telemetry", action = "LogFeedback", authenticated = false }, new { verb = new HttpMethodConstraint("POST") }); //Resources Api Routes RouteTable.Routes.MapHttpRoute("get-resource", "api/resource", new { controller = "Resource", action = "GetResource", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("get-username", "api/resource/user", new { controller = "Resource", action = "GetUserIdentityName", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("create-resource", "api/resource", new { controller = "Resource", action = "CreateResource", authenticated = true }, new { verb = new HttpMethodConstraint("POST") }); RouteTable.Routes.MapHttpRoute("get-webapp-publishing-profile", "api/resource/getpublishingprofile", new { controller = "Resource", action = "GetWebAppPublishingProfile", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("get-mobile-client-app", "api/resource/mobileclient/{platformString}", new { controller = "Resource", action = "GetMobileClientZip", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("delete-resource", "api/resource", new { controller = "Resource", action = "DeleteResource", authenticated = true }, new { verb = new HttpMethodConstraint("DELETE") }); RouteTable.Routes.MapHttpRoute("get-resource-status", "api/resource/status", new { controller = "Resource", action = "GetResourceStatus", authenticated = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("extend-resource-expiration-time", "api/resource/extend", new { controller = "Resource", action = "ExtendResourceExpirationTime", authenticated = true }, new { verb = new HttpMethodConstraint("POST") }); //Admin Only Routes RouteTable.Routes.MapHttpRoute("get-all-resources", "api/resource/all/{showFreeSites}", new { controller = "Resource", action = "All", authenticated = true, adminOnly = true, showFreeSites = RouteParameter.Optional }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("reset-all-free-resources", "api/resource/reset", new { controller = "Resource", action = "Reset", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("reload-all-free-resources", "api/resource/reload", new { controller = "Resource", action = "DropAndReloadFromAzure", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); RouteTable.Routes.MapHttpRoute("delete-users-resource", "api/resource/delete/{userIdentity}", new { controller = "Resource", action = "DeleteUserResource", authenticated = true, adminOnly = true }, new { verb = new HttpMethodConstraint("GET") }); //Register auth provider SecurityManager.InitAuthProviders(); ResourcesManager.GetInstanceAsync().ConfigureAwait(false).GetAwaiter().GetResult(); }