Пример #1
0
        private UniqueLogger CreateNewLogger(string uniqueId)
        {
            var loggingSwitch = new LoggingLevelSwitch();
            var logger        = new LoggerConfiguration()
                                .WithIdentity(uniqueId)
                                .MinimumLevel.Verbose()
                                .WriteTo.Logger(subLogger =>
                                                subLogger.MinimumLevel.ControlledBy(loggingSwitch)
                                                // .WriteTo.Console (outputTemplate:"[{Timestamp:HH:mm:ss} {Level:u3} {Identity}] {Message:lj}{NewLine}{Exception}")
                                                .WriteTo.File(
                                                    $"logs/plugin_{uniqueId}_.log",
                                                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Identity} {Level:u3}] {Message:lj}{NewLine}{Exception}",
                                                    fileSizeLimitBytes: 100_000,
                                                    rollOnFileSizeLimit: true,
                                                    rollingInterval: RollingInterval.Hour,
                                                    retainedFileCountLimit: 50,
                                                    buffered: true,
                                                    flushToDiskInterval: TimeSpan.FromSeconds(30)
                                                    )
                                                )
                                .WriteTo.Logger(_defaultLogger)
                                .CreateLogger();

            return(new UniqueLogger(uniqueId, logger, loggingSwitch));
        }
Пример #2
0
        public static void Main(string[] args)
        {
            var db = documentClient.GetDatabase("log");

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .Enrich.WithProperty("App Name", "API Core")
                         .Enrich.FromLogContext()
                         .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.WithProperty("AuditLog"))
                                         .WriteTo.Console(new ElasticsearchJsonFormatter())
                                         .Enrich.WithThreadName()

                                         //.WriteTo.MongoDB(db,collectionName:"apiFull")
                                         .WriteTo.Debug()
                                         .WriteTo.File(@"d:\log\serilog\logCore.txt", fileSizeLimitBytes: 1_000_000,
                                                       rollOnFileSizeLimit: true, shared: true,
                                                       flushToDiskInterval: TimeSpan.FromSeconds(1))
                                         )
                         .WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(Matching.WithProperty("AuditLog"))
                                         .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
            {
                AutoRegisterTemplate        = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
                //PipelineName = "ApiCoreTestPipeline",
                //TypeName = "ApiCoreTesType"
            })
                                         .WriteTo.MongoDB(db, collectionName: "logCore")
                                         )
                         .CreateLogger();
            try
            {
                Log.Information("Starting API Core Serilog");

                CreateHostBuilder(args).Build().Run();
                return;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
Пример #3
0
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json", optional: false)
                         .AddJsonFile("hostsettings.json", optional: true)
                         .AddCommandLine(args)
                         .Build();

            var logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .WriteTo.Console()
                         .WriteTo.Logger(x =>
            {
                if (config.GetSection("Logging").GetValue <bool>("LogToFile"))
                {
                    x.WriteTo.File("information.log", rollOnFileSizeLimit: true, fileSizeLimitBytes: 20_971_520);
                    x.Filter.ByExcluding(x => x.Level == Serilog.Events.LogEventLevel.Error);
                    x.Filter.ByExcluding(x => x.Level == Serilog.Events.LogEventLevel.Fatal);
                    x.Filter.ByExcluding(x => x.Level == Serilog.Events.LogEventLevel.Warning);
                }
            })
                         .WriteTo.Logger(x =>
            {
                x.WriteTo.File("error.log");
                x.MinimumLevel.Warning();
            })
                         .CreateLogger();

            return(Host.CreateDefaultBuilder(args)
                   .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup <Startup>();
                webBuilder.UseUrls("http://*:50699");
                webBuilder.UseConfiguration(config);
                webBuilder.UseSerilog(logger);
            }));
        }