public void WillCorrectlyAssignAllConstructorArguments()
        {
            var options = new GoogleCloudLoggingSinkOptions(projectId: "projectId",
                                                            resourceType: "k8s_pod",
                                                            logName: "logName",
                                                            labels: new Dictionary <string, string> {
                { "labelKey", "label value" }
            },
                                                            resourceLabels: new Dictionary <string, string> {
                { "resourceKey", "resource value" }
            },
                                                            useSourceContextAsLogName: false,
                                                            useJsonOutput: true,
                                                            googleCredentialJson: "{}",
                                                            serviceName: "service-name",
                                                            serviceVersion: "1.0.1");

            options
            .Should()
            .BeEquivalentTo(new GoogleCloudLoggingSinkOptions
            {
                GoogleCredentialJson = "{}",
                Labels                    = { { "labelKey", "label value" } },
                LogName                   = "logName",
                ProjectId                 = "projectId",
                ResourceLabels            = { { "resourceKey", "resource value" } },
                ResourceType              = "k8s_pod",
                ServiceName               = "service-name",
                ServiceVersion            = "1.0.1",
                UseJsonOutput             = true,
                UseSourceContextAsLogName = false,
            });
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            // environment-variables-configuration-provider by default only gets env variables from ASPNET_<variable> variables. add with the below.
            // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.2#environment-variables-configuration-provider
            var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
            var googleCloudLoggingConfig = new GoogleCloudLoggingSinkOptions
            {
                ProjectId     = config["GOOGLE_PROJECT_ID"],
                UseJsonOutput = true
            };

            Log.Logger = new LoggerConfiguration().MinimumLevel.Information().Enrich.FromLogContext().WriteTo
                         .Console(
                outputTemplate:
                "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} -- Properties: {Properties:j} {NewLine}{Exception}"
                ).WriteTo.GoogleCloudLogging(googleCloudLoggingConfig).CreateLogger();
            try
            {
                Log.Information("Starting web host...");
                CreateWebHostBuilder(args).Build().Run();
                return(0);
            }
            catch (Exception exception)
            {
                Log.Fatal(exception, "Host terminated suddenly.");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        private static void SetOptionsProgrammatically()
        {
            var options = new GoogleCloudLoggingSinkOptions("PROJECT-ID-HERE-12345")
            {
                ResourceType = "k8s_cluster",
                LogName      = "someLogName",
                UseSourceContextAsLogName = false,
                UseJsonOutput             = true
            };

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.GoogleCloudLogging(options)    // Add this to send Serilog output to GCP
                         .MinimumLevel.Is(LogEventLevel.Verbose) // Serilog defaults to Info level and above, use this to override
                         .CreateLogger();
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

            var options = new GoogleCloudLoggingSinkOptions(GCP_PROJECT_ID);

            options.UseSourceContextAsLogName = false;
            options.UseJsonOutput             = true;

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.GoogleCloudLogging(options)    // Add this to send Serilog output to GCP
                         .MinimumLevel.Is(LogEventLevel.Verbose) // Serilog defaults to Info level and above, use this to override
                         .CreateLogger();

            BuildWebHost(args).Run();
        }
Ejemplo n.º 5
0
        private static Serilog.ILogger ProductionLogging <TImplementingType>(AppSettings appSettings, LoggerSettings loggerSettings)
        {
            var googleCredentialJson = GoogleCredentialFactory.GetGoogleCredentialJson();

            var config = new GoogleCloudLoggingSinkOptions {
                ProjectId = appSettings.GcpProjectId, UseJsonOutput = true, GoogleCredentialJson = googleCredentialJson
            };

            return(new LoggerConfiguration()
                   .MinimumLevel.Is(MapLogEventLevel(loggerSettings.LogEventLevel))
                   .Enrich.FromLogContext()
                   .WriteTo.GoogleCloudLogging(config)
                   .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                   .MinimumLevel.Override("System", LogEventLevel.Warning)
                   .CreateLogger()
                   .ForContext(typeof(TImplementingType)));
        }
Ejemplo n.º 6
0
        public static void Register(string name)
        {
            var config = new GoogleCloudLoggingSinkOptions
            {
                UseJsonOutput             = true,
                LogName                   = "api.mapserv.utah.gov",
                UseSourceContextAsLogName = false,
                ResourceType              = "global",
                ServiceName               = "api.mapserv.utah.gov",
                ServiceVersion            = "1.12.5.5"
            };

#if DEBUG
            var projectId      = "ut-dts-agrc-web-api-dv";
            var fileName       = "ut-dts-agrc-web-api-dv-log-writer.json";
            var serviceAccount = File.ReadAllText(Path.Combine(HttpRuntime.AppDomainAppPath, fileName));
            config.GoogleCredentialJson = serviceAccount;
#else
            var projectId = "ut-dts-agrc-web-api-prod";
#endif
            config.ProjectId = projectId;

            var email = new EmailConnectionInfo
            {
                EmailSubject = "Geocoding Log Email",
                FromEmail    = "*****@*****.**",
                ToEmail      = "*****@*****.**"
            };

            var levelSwitch = new LoggingLevelSwitch();

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.ControlledBy(levelSwitch)
                         .WriteTo.Email(email, restrictedToMinimumLevel: LogEventLevel.Error)
                         .WriteTo.GoogleCloudLogging(config)
                         .CreateLogger();

#if DEBUG
            levelSwitch.MinimumLevel = LogEventLevel.Verbose;
#else
            levelSwitch.MinimumLevel = LogEventLevel.Information;
#endif

            Log.Debug("Logging initialized");
        }