示例#1
0
 public static Task <LogManager> StartEverything()
 {
     return(LogaryFactory.New("Logary.CSharpExample", "localhost",
                              with => with.InternalLogger(ILogger.NewConsole(LogLevel.Debug))
                              // .Metrics(m =>
                              //     m.AddMetric(Duration.FromSeconds(3L), "appMetrics", WinPerfCounters.appMetrics)
                              //      .AddMetric(Duration.FromSeconds(3L), "systemMetrics", WinPerfCounters.systemMetrics))
                              .Target <TextWriter.Builder>(
                                  "console1",
                                  conf =>
                                  conf.Target.WriteTo(System.Console.Out, System.Console.Error)
                                  .MinLevel(LogLevel.Verbose)
                                  .AcceptIf(line => true)
                                  .SourceMatching(new Regex(".*"))
                                  )
                              .Target <Graphite.Builder>(
                                  "graphite",
                                  conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                  )
                              .Target <Debugger.Builder>("debugger")
                              .Target <ElasticSearch.Builder>(
                                  "es",
                                  conf => conf.Target
                                  .PublishTo("elasticsearch.service")
                                  .Type("logs") // index-name
                                  .Done()
                                  )
                              //.Target<File.Builder>(
                              //    "file",
                              //    conf => conf
                              //)
                              .Target <InfluxDb.Builder>("influx",
                                                         conf => conf.Target.DB("http://influxdb.service:8086").Done())
                              ));
 }
示例#2
0
 public static Task <LogManager> Run()
 {
     return(LogaryFactory.New("Logary.CSharpExample", "localhost",
                              with => with.InternalLogger(ILogger.NewConsole(LogLevel.Debug))
                              .Target <TextWriter.Builder>(
                                  "console1",
                                  conf =>
                                  conf.Target.WriteTo(System.Console.Out, System.Console.Error)
                                  .MinLevel(LogLevel.Verbose)
                                  .AcceptIf(line => true)
                                  .SourceMatching(new Regex(".*"))
                                  )
                              .Target <Graphite.Builder>(
                                  "graphite",
                                  conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                  )
                              .Target <Debugger.Builder>("debugger")
                              .Target <Elasticsearch.Builder>(
                                  "es",
                                  conf => conf.Target
                                  .PublishTo("elasticsearch.service")
                                  .Type("logs") // index-name
                                  .Done()
                                  )
                              .Target <InfluxDb.Builder>("influx", conf => conf.Target.DB("http://influxdb.service:8086").Done())
                              ));
 }
示例#3
0
 public static Task <LogManager> StartLiterate()
 {
     return(LogaryFactory.New("Logary.CSharpExample",
                              with => with
                              .InternalLoggingLevel(LogLevel.Debug)
                              .Target <LiterateConsole.Builder>("console1")));
 }
示例#4
0
        public static LogManager ConfigureLogary(string serviceName, string logPath, string errorLogPath, string logStashHostName, ushort logStashPort)
        {
            var stream = File.Open(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            var writer = new StreamWriter(stream);

            var errorStream = File.Open(errorLogPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            var errorWriter = new StreamWriter(errorStream);

#if DEBUG
            var x = LogaryFactory.New(serviceName, with =>
                                      with
                                      .Target <Logary.Targets.TextWriter.Builder>("LogFile",
                                                                                  conf => conf.Target.WriteTo(writer, errorWriter))

                                      .Target <Logary.Targets.TextWriter.Builder>("Console",
                                                                                  conf => conf.Target.WriteTo(Console.Out, Console.Error)));
#else
            var x = LogaryFactory.New(serviceName, with =>
                                      with
                                      .Target <Logary.Targets.TextWriter.Builder>("LogFile",
                                                                                  conf => conf.Target.WriteTo(writer, errorWriter))

                                      .Target <Logary.Targets.TextWriter.Builder>("Console",
                                                                                  conf => conf.Target.WriteTo(Console.Out, Console.Error))

                                      .Target <Logstash.Builder>("LogStash",
                                                                 conf => conf.Target
                                                                 .Hostname(logStashHostName)
                                                                 .Port(logStashPort)
                                                                 .EventVersion(Logstash.EventVersion.One)
                                                                 .Done()));
#endif

            return(x);
        }
示例#5
0
        protected override void Load(ContainerBuilder builder)
        {
            var serviceName = Assembly.GetEntryAssembly().GetName().Name;

            builder.Register(context => LogaryFactory.New(serviceName, with =>
                                                          with.Target <TextWriter.Builder>("console", conf => conf.Target.WriteTo(Console.Out, Console.Error)
                                                                                           .MinLevel(LogLevel.Verbose)
                                                                                           .AcceptIf(line => true)
                                                                                           .SourceMatching(new Regex(".*")))))
            .AsSelf();
        }
示例#6
0
        public static int Main(string[] args)
        {
            using (var logary = LogaryFactory.New("Logary.CSharpExample",
                                                  with => with
                                                  .Target <Targets.ElmahIO.Builder>(
                                                      "elmah.io",
                                                      conf => conf.Target.WithLogId(Guid.Parse(Environment.GetEnvironmentVariable("ELMAH_IO_LOG_ID"))))
                                                  ).Result)
            {
                var logger = logary.GetLogger("Logary.CSharpExample");

                logger.LogEvent(LogLevel.Info, "Hello world", new
                {
                    important = "yes"
                });

                logger.LogEvent(LogLevel.Fatal, "Fatal application error on finaliser thread");

                logger.LogEvent(LogLevel.Verbose, "immegawd immegawd immegawd!!", new
                {
                    tags = new[] { "tag1", "tag2" }
                });

                var val = logger.TimePath("sample.config.computeAnswerToEverything", () =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        System.Threading.Thread.Sleep(1);
                    }

                    return(32);
                });

                logger.LogEventFormat(LogLevel.Warn, "{0} is the answer to the universe and everything", val);

                logger.Time(() => logger.LogEvent(LogLevel.Debug, "I wonder how long this takes"));

                try
                {
                    throw new ApplicationException("thing went haywire");
                }
                catch (Exception e)
                {
                    logger.LogEventFormat(LogLevel.Fatal, "Unhandled {exception}!", e);
                }
            }
            return(0);
        }
示例#7
0
 public static int Main(string[] args)
 {
     using (var logary = LogaryFactory.New("Logary.CSharpExample",
                                           with => with.InternalLoggingLevel(LogLevel.Debug)
                                           .Metrics(m =>
                                                    m.AddMetric(Duration.FromSeconds(3L), "appMetrics", WinPerfCounters.appMetrics)
                                                    .AddMetric(Duration.FromSeconds(3L), "systemMetrics", WinPerfCounters.systemMetrics))
                                           .Target <TextWriter.Builder>(
                                               "console1",
                                               conf =>
                                               conf.Target.WriteTo(System.Console.Out, System.Console.Error)
                                               .MinLevel(LogLevel.Verbose)
                                               .AcceptIf(line => true)
                                               .SourceMatching(new Regex(".*"))
                                               )
                                           .Target <Graphite.Builder>(
                                               "graphite",
                                               conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                               )
                                           .Target <Debugger.Builder>("debugger")
                                           .Target <Logstash.Builder>(
                                               "logstash",
                                               conf => conf.Target
                                               .PublishTo("logstash.service")
                                               .LogMetrics()
                                               .Done()
                                               )
                                           .Target <ElasticSearch.Builder>(
                                               "es",
                                               conf => conf.Target
                                               .PublishTo("elasticsearch.service")
                                               .Type("logs") // index-name
                                               .Done()
                                               )
                                           //.Target<File.Builder>(
                                           //    "file",
                                           //    conf => conf
                                           //)
                                           .Target <InfluxDb.Builder>("influx",
                                                                      conf => conf.Target.DB("http://influxdb.service:8086").Done())
                                           ).Result)
     {
         var logger = logary.GetLogger("Logary.CSharpExample");
         SampleUsage(logger).Wait();
     }
     return(0);
 }
示例#8
0
文件: Program.cs 项目: logary/logary
        public static async Task Main(string[] args)
        {
            var logary = await LogaryFactory.New("Logary.AspNetCore.API", config =>
                                                 config
                                                 .InternalLogger(ILogger.NewLiterateConsole(LogLevel.Verbose))
                                                 .LoggerMinLevel(".*", LogLevel.Verbose)
                                                 .Target <LiterateConsole.Builder>(
                                                     "literate",
                                                     lit => lit.Target.WithSingleLineTokeniser().Done())
                                                 .Target <Logary.Targets.Jaeger.Builder>(
                                                     "jaeger",
                                                     x =>
                                                     x.Target
                                                     .WithJaegerAgent("localhost", 30831)
                                                     .WithSampler(new PerKeySampler(0.2, 100))
                                                     .Done())
                                                 );

            CreateWebHostBuilder(args, logary).Build().Run();
        }
示例#9
0
        public static int Main(string[] args)
        {
            var logId = Environment.GetEnvironmentVariable("ELMAH_IO_LOG_ID");

            if (logId == null)
            {
                throw new InvalidOperationException("Missing key 'ELMAH_IO_LOG_ID' from environment");
            }

            using (var logary = LogaryFactory.New("Logary.ElmahIO Sample",
                                                  with => with
                                                  .Target <Targets.ElmahIO.Builder>(
                                                      "elmah.io",
                                                      conf => conf.Target.WithLogId(Guid.Parse(logId)))
                                                  ).Result)
            {
                var logger = logary.GetLogger("Logary.ElmahIOSample");
                Sample(logger).Wait();
            }
            return(0);
        }
示例#10
0
        public void UsageExample()
        {
            var x = LogaryFactory.New("Logary Specs",
                                      with => with.Target <TextWriter.Builder>(
                                          "console1",
                                          conf =>
                                          conf.Target.WriteTo(Console.Out, Console.Error)
                                          .MinLevel(LogLevel.Verbose)
                                          .AcceptIf(line => true)
                                          .SourceMatching(new Regex(".*"))
                                          )
                                      .Target <Graphite.Builder>(
                                          "graphite",
                                          conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                          )
                                      .Target <Debugger.Builder>("debugger")
                                      .Target <Logstash.Builder>(
                                          "ls",
                                          conf => conf.Target
                                          .Hostname("localhost")
                                          .Port(1936)
                                          .EventVersion(Logstash.EventVersion.One)
                                          .Done())
                                      .Target <DB.Builder>("db",
                                                           conf => conf.Target
                                                           .ConnectionFactory(() => new SQLiteConnection())
                                                           .DefaultSchema()
                                                           .MigrateUp(
                                                               conn => new SqliteProcessor(conn,
                                                                                           new SqliteGenerator(),
                                                                                           new ConsoleAnnouncer(),
                                                                                           new MigrationOptions(false, "", 60),
                                                                                           new SqliteDbFactory())))
                                      );

            var logger = x.GetLogger("Sample.Config");

            logger.Log("Hello world", LogLevel.Debug, new
            {
                important = "yes"
            });

            logger.Log(LogLevel.Fatal, "Fatal application error on finaliser thread");

            logger.Verbose("immegawd immegawd immegawd!!", "tag1", "tag2");

            var val = logger.TimePath("sample.config.compute_answer_to_everything", () =>
            {
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(1);
                }

                return(32);
            });

            logger.LogFormat(LogLevel.Warn, "{0} is the answer to the universe and everything", val);

            logger.Time(() => logger.Debug("I wonder how long this takes", "introspection", "navel-gazing"));

            try
            {
                throw new ApplicationException("thing went haywire");
            }
            catch (Exception e)
            {
                logger.DebugException("expecting haywire, so we're telling with debug", e, "haywire", "external");
            }
        }
示例#11
0
        public async Task UsageExample()
        {
            var x = await LogaryFactory.New("Logary.CSharp.Tests.Examples",
                                            with => with.Target <TextWriter.Builder>(
                                                "console1",
                                                conf =>
                                                conf.Target.WriteTo(Console.Out, Console.Error)
                                                .MinLevel(LogLevel.Verbose)
                                                .AcceptIf(line => true)
                                                .SourceMatching(new Regex(".*"))
                                                )
                                            .Target <Graphite.Builder>(
                                                "graphite",
                                                conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                                )
                                            .Target <Debugger.Builder>("debugger")
                                            .Target <LiterateConsole.Builder>("literate")
                                            .Target <Logstash.Builder>(
                                                "ls",
                                                conf => conf.Target
                                                .PublishTo("tcp://192.168.100.99:5001")
                                                .Done())
                                            //.Target<Logary.Targets.DB.Builder>("db",
                                            //    conf => conf.Target
                                            //       .ConnectionFactory(() => new SQLiteConnection())
                                            //        .DefaultSchema()
                                            //        .MigrateUp(
                                            //            conn => new SqliteProcessor(conn,
                                            //                new SqliteGenerator(),
                                            //                new ConsoleAnnouncer(),
                                            //                new MigrationOptions(false, "", 60),
                                            //                new SqliteDbFactory())))
                                            );

            var logger = x.GetLogger("Sample.Config");

            await logger.LogEvent(LogLevel.Debug, "Hello world", new
            {
                important = "yes"
            });

            await logger.LogEvent(LogLevel.Fatal, "Fatal application error on finaliser thread");

            await logger.LogEvent(LogLevel.Verbose, "immegawd immegawd immegawd!!", new {
                tags = new [] { "tag1", "tag2" }
            });

            using (logger.TimeScope("TimeScope"))
                Thread.Sleep(0);

            var val = logger.Time(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(1);
                }

                return(32);
            }, "compute_answer_to_everything")
                          ();

            await logger.LogEventFormat(LogLevel.Warn, "{theAnswer} is the answer to the universe and everything", val);

            await logger.Time(() => logger.LogEvent(LogLevel.Debug, "I wonder how long this takes", new
            {
                tags = new[] { "introspection", "navel-gazing" }
            }))
                ();

            try
            {
                throw new ApplicationException("thing went haywire");
            }
            catch (Exception e)
            {
                await logger.LogEvent(LogLevel.Fatal, "expecting haywire, so we're telling with debug", new {
                    tags = new[] { "haywire", "external" },
                }, exn : e);
            }
        }
示例#12
0
        public static int Main(string[] args)
        {
            using (var logary = LogaryFactory.New("Logary.CSharpExample",
                                                  with => with.Target <TextWriter.Builder>(
                                                      "console1",
                                                      conf =>
                                                      conf.Target.WriteTo(System.Console.Out, System.Console.Error)
                                                      .MinLevel(LogLevel.Verbose)
                                                      .AcceptIf(line => true)
                                                      .SourceMatching(new Regex(".*"))
                                                      )
                                                  .Target <Graphite.Builder>(
                                                      "graphite",
                                                      conf => conf.Target.ConnectTo("127.0.0.1", 2131)
                                                      )
                                                  .Target <Debugger.Builder>("debugger")
                                                  .Target <Logstash.Builder>(
                                                      "logstash",
                                                      conf => conf.Target
                                                      .PublishTo("logstash.service")
                                                      .LogMetrics()
                                                      .Done()
                                                      )
                                                  .Target <ElasticSearch.Builder>(
                                                      "es",
                                                      conf => conf.Target
                                                      .PublishTo("elasticsearch.service")
                                                      .Type("logs") // index-name
                                                      .Done()
                                                      )
                                                  .Target <Logary.Targets.RabbitMQ.Builder>(
                                                      "rabbitmq",
                                                      conf => conf.Target.EnableTls("./cert/path.pfx", "TopSecret12345").Done()
                                                      )
                                                  // currently Windows only:

                                                  /*
                                                   *  .Target<DB.Builder>("db",
                                                   *      conf => conf.Target
                                                   *          .ConnectionFactory(() => new SQLiteConnection())
                                                   *          .DefaultSchema()
                                                   *          .MigrateUp(
                                                   *              conn => new SqliteProcessor(conn,
                                                   *                  new SqliteGenerator(),
                                                   *                  new ConsoleAnnouncer(),
                                                   *                  new MigrationOptions(false, "", 60),
                                                   *                                          new SqliteDbFactory())))
                                                   */
                                                  ).Result)
            {
                var logger = logary.GetLogger("Logary.CSharpExample");

                logger.LogEvent(LogLevel.Debug, "Hello world", new
                {
                    important = "yes"
                });

                logger.LogEvent(LogLevel.Fatal, "Fatal application error on finaliser thread");

                logger.LogEvent(LogLevel.Verbose, "immegawd immegawd immegawd!!", new
                {
                    tags = new[] { "tag1", "tag2" }
                });

                var val = logger.TimePath("sample.config.computeAnswerToEverything", () =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        System.Threading.Thread.Sleep(1);
                    }

                    return(32);
                });

                logger.LogEventFormat(LogLevel.Warn, "{0} is the answer to the universe and everything", val);

                logger.Time(() => logger.LogEvent(LogLevel.Debug, "I wonder how long this takes"));

                try
                {
                    throw new ApplicationException("thing went haywire");
                }
                catch (Exception e)
                {
                    logger.LogEventFormat(LogLevel.Fatal, "Unhandled {exception}!", e);
                }
            }
            return(0);
        }