Пример #1
0
        internal static string FormatCompactPayload(IEnumerable <LogEvent> events, long?eventBodyLimitBytes)
        {
            var payload = new StringWriter();

            foreach (var logEvent in events)
            {
                var buffer = new StringWriter();

                try
                {
                    CompactJsonFormatter.FormatEvent(logEvent, buffer, JsonValueFormatter);
                }
                catch (Exception ex)
                {
                    LogNonFormattableEvent(logEvent, ex);
                    continue;
                }

                var json = buffer.ToString();
                if (CheckEventBodySize(json, eventBodyLimitBytes))
                {
                    payload.WriteLine(json);
                }
            }

            return(payload.ToString());
        }
Пример #2
0
        private TReturn Query <TReturn>(string apiEndPoint, RestParameterSet parameter, int maxTries)
        {
            var para = parameter.CreateParamString(secret);


            string url = serverbasepath + "/" + apiEndPoint + ".php" + para.Item1;

#if DEBUG
            if (DebugSettings.Get("DebugNetwork"))
            {
                SAMLog.Debug($"QueryAsync('{apiEndPoint}', '{url}', {maxTries})");
            }
#endif

            for (;;)
            {
                try
                {
                    HttpResponseMessage response;
                    if (para.Item2 != null)
                    {
                        response = http.PostAsync(url, para.Item2).Result;
                    }
                    else
                    {
                        response = http.GetAsync(url).Result;
                    }

                    response.EnsureSuccessStatusCode();
                    var content = response.Content.ReadAsStringAsync().Result;
#if DEBUG
                    if (DebugSettings.Get("DebugNetwork"))
                    {
                        var json   = CompactJsonFormatter.CompressJson(content, 1);
                        var jlines = json.Replace("\r\n", "\n").Split('\n');
                        if (jlines.Length > 7)
                        {
                            json = string.Join("\n", jlines.Take(3).Concat(new[] { "..." }).Concat(jlines.Reverse().Take(3).Reverse()));
                        }
                        SAMLog.Debug($"Query '{apiEndPoint}' returned \r\n" + json);
                    }
#endif
                    return(JsonConvert.DeserializeObject <TReturn>(content));
                }
                catch (Exception e)
                {
                    if (maxTries > 0)
                    {
                        maxTries--;
                        SAMLog.Info("QueryAsync", $"Retry query '{url}'. {maxTries}remaining", e.Message);
                        MonoSAMGame.CurrentInst.Bridge.Sleep(RETRY_SLEEP_TIME);
                        continue;
                    }
                    else
                    {
                        throw new RestConnectionException(e);                         // return to sender
                    }
                }
            }
        }
Пример #3
0
        private static Logger OverrideEverything(string url)
        {
            ITextFormatter textFormatter = new CompactJsonFormatter();

            return(new LoggerConfiguration()
                   .WriteTo.Console()
                   .WriteTo.Async(configuration =>
                                  configuration.SumoLogic(
                                      url,
                                      sourceName: "CustomSourceName",
                                      sourceCategory: "CustomSourceCategory",
                                      restrictedToMinimumLevel: LogEventLevel.Debug,
                                      batchSizeLimit: 20,
                                      period: TimeSpan.FromSeconds(1),
                                      textFormatter: new CompactJsonFormatter()))   // use in console apps
                   .WriteTo.SumoLogic(
                       url,
                       sourceName: "CustomSourceName",
                       sourceCategory: "CustomSourceCategory",
                       restrictedToMinimumLevel: LogEventLevel.Debug,
                       batchSizeLimit: 20,
                       period: TimeSpan.FromSeconds(1),
                       textFormatter: new CompactJsonFormatter())                    // use in ASP.NET
                   .CreateLogger());
        }
Пример #4
0
        private void Configure(IApplicationBuilder app)
        {
            var formatter = new CompactJsonFormatter();

            app.Run(
                ctx =>
            {
                if (ctx.Request.Method == "GET")
                {
                    ctx.Response.StatusCode  = 200;
                    ctx.Response.ContentType = "application/vnd.serilog.clef";

                    using (var writer = new StreamWriter(ctx.Response.Body, System.Text.Encoding.UTF8))
                    {
                        while (this.logEvents.TryPop(out var logEvent))
                        {
                            formatter.Format(logEvent, writer);
                        }
                    }

                    ctx.Response.Body.Flush();
                }
                else if (ctx.Request.Method == "DELETE")
                {
                    this.logEvents.Clear();
                }
                else
                {
                    ctx.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
                }

                return(Task.CompletedTask);
            });
        }
Пример #5
0
        public static LoggerConfiguration AppendAwsCloudwatchLogger(this LoggerConfiguration logger,
                                                                    string logGroupName,
                                                                    string environmentName,
                                                                    LogEventLevel minimumLogEvent)
        {
            logGroupName = $"{logGroupName}/{environmentName}".ToLower();

            var formatter = new CompactJsonFormatter();
            var options   = new CloudWatchSinkOptions
            {
                LogGroupName         = logGroupName,
                TextFormatter        = formatter,
                MinimumLogEventLevel = minimumLogEvent,
                BatchSizeLimit       = 100,
                QueueSizeLimit       = 10000,
                Period                  = TimeSpan.FromSeconds(10),
                CreateLogGroup          = true,
                LogStreamNameProvider   = new DefaultLogStreamProvider(),
                RetryAttempts           = 5,
                LogGroupRetentionPolicy = LogGroupRetentionPolicy.OneDay
            };

            var client = new AmazonCloudWatchLogsClient(RegionEndpoint.SAEast1);

            return(logger
                   //.Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))
                   .WriteTo.AmazonCloudWatch(options, client));
        }
        public void ConfigureLogging(LoggerConfiguration loggerConfiguration, HostingEnvironment hostingEnvironment)
        {
            var fileFormatter = new CompactJsonFormatter();

            //Serilog.Core.Logger auditLogger = new LoggerConfiguration()
            //    .Filter.ByIncludingOnly(Matching.FromSource<Audit>())
            //    .AuditTo.File(fileFormatter, "audit-logger.json")
            //    .CreateLogger();

            Serilog.Core.Logger applicationLogger = new LoggerConfiguration()
                                                    .Filter().Overrides(globalSwitches)
                                                    .WriteTo.File(fileFormatter, "application-logger.json", shared: true)
                                                    .CreateLogger();

            loggerConfiguration
            .MinimumLevel.Verbose()
            .Enrich.FromLogContext()
            //.AuditTo.Logger(auditLogger)
            .WriteTo.Logger(applicationLogger)
            .WriteTo.Console(
                outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Properties:j}{NewLine}{Exception}",
                theme: GetTheme(hostingEnvironment)
                )
            .WriteTo.Seq(serverUrl: "http://localhost:5341", compact: true);
        }
Пример #7
0
        internal static string FormatCompactPayload(LogEvent logEvent)
        {
            var payload = new StringWriter();

            CompactJsonFormatter.FormatEvent(logEvent, payload, JsonValueFormatter);
            payload.WriteLine();
            return(payload.ToString());
        }
Пример #8
0
        private static Logger CustomTextFormatter(string url)
        {
            ITextFormatter textFormatter = new CompactJsonFormatter();

            return(new LoggerConfiguration()
                   .WriteTo.Console()
                   .WriteTo.Async(configuration =>
                                  configuration.SumoLogic(url, "CustomTextFormatter", textFormatter: textFormatter)) // use in console apps
                   .WriteTo.SumoLogic(url, "CustomTextFormatter", textFormatter: textFormatter)                      // use in ASP.NET
                   .CreateLogger());
        }
Пример #9
0
        public static LoggerConfiguration SuperRollingFileAlternate(
            this LoggerSinkConfiguration configuration,
            string logDirectory,
            string logFilePrefix = "",
            SuperRollingFileAlternateFormats format = SuperRollingFileAlternateFormats.Text,
            ITextFormatter formatter   = null,
            LogEventLevel minimumLevel = LevelAlias.Minimum,
            long fileSizeLimitBytes    = DefaultFileSizeLimitBytes,
            int?retainedFileCountLimit = DefaultRetainedFileCountLimit,
            Encoding encoding          = null,
            bool renderMessage         = false,
            bool async                     = false,
            string outputTemplate          = DefaultOutputTemplate,
            IFormatProvider formatProvider = null)
        {
            if (formatter == null)
            {
                switch (format)
                {
                case SuperRollingFileAlternateFormats.Json:
                    formatter = new JsonFormatter(renderMessage: renderMessage);
                    break;

                case SuperRollingFileAlternateFormats.CompactJson:
                    formatter = new CompactJsonFormatter();
                    break;

                case SuperRollingFileAlternateFormats.Text:
                default:
                    formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
                    break;
                }
            }
            var sink = new AlternateRollingFileSink(logDirectory, formatter, fileSizeLimitBytes,
                                                    retainedFileCountLimit, encoding, logFilePrefix);

            if (async)
            {
                return(configuration.Async(c => c.Sink(sink, minimumLevel)));
            }
            else
            {
                return(configuration.Sink(sink, minimumLevel));
            }
        }
Пример #10
0
        private ILogger CreateLogger(string gameDirectory, LoggingConfiguration loggingConfiguration, ForwardingTextWriter logTextWriter)
        {
            var config = new LoggerConfiguration();

            config.MinimumLevel.Verbose();

            ITextFormatter fileFormatter = null;

            switch (loggingConfiguration.LogFormat)
            {
            case LoggingConfiguration.Format.Text:
            {
                fileFormatter = new MessageTemplateTextFormatter("{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}", null);
                break;
            }

            case LoggingConfiguration.Format.CompactJSON:
            {
                fileFormatter = new CompactJsonFormatter();
                break;
            }
            }

            //Invalid config setting for RetainedFileCountLimit will throw
            config
            .WriteTo.File(fileFormatter, $"{gameDirectory}/logs/engine.log",
                          rollingInterval: RollingInterval.Day,
                          retainedFileCountLimit: loggingConfiguration.RetainedFileCountLimit);

            //Use basic formatting for console output
            var logFormatter = new MessageTemplateTextFormatter("{Message:lj}{NewLine}{Exception}", null);

            if (logTextWriter != null)
            {
                config.WriteTo.TextWriter(logFormatter, logTextWriter);
            }

            return(config.CreateLogger());
        }
Пример #11
0
        public static LoggerConfiguration AppendAwsCloudwatchLogger(this LoggerConfiguration logger, string logGroupName, LogEventLevel minimumLogEvent, string accessKey, string securityKey)
        {
            // customer formatter
            var formatter = new CompactJsonFormatter();

            // options for the sink defaults in https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch/blob/master/src/Serilog.Sinks.AwsCloudWatch/CloudWatchSinkOptions.cs
            var options = new CloudWatchSinkOptions
            {
                LogGroupName          = logGroupName,
                TextFormatter         = formatter,
                MinimumLogEventLevel  = minimumLogEvent,
                LogStreamNameProvider = new DefaultLogStreamProvider(),
                RetryAttempts         = 5
            };

            var credentials = new BasicAWSCredentials(accessKey, securityKey);

            // setup AWS CloudWatch client
            var client = new AmazonCloudWatchLogsClient(credentials, RegionEndpoint.EUWest1);

            return(logger
                   .WriteTo.AmazonCloudWatch(options, client));
        }
        /// <remarks>
        ///    Used in config - If renamed or moved to other assembly the config file also has be updated.
        /// </remarks>
        public static LoggerConfiguration UmbracoFile(this LoggerSinkConfiguration configuration,
                                                      string path,
                                                      ITextFormatter formatter = null,
                                                      LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
                                                      LoggingLevelSwitch levelSwitch         = null,
                                                      long?fileSizeLimitBytes         = 1073741824,
                                                      TimeSpan?flushToDiskInterval    = null,
                                                      RollingInterval rollingInterval = RollingInterval.Day,
                                                      bool rollOnFileSizeLimit        = false,
                                                      int?retainedFileCountLimit      = 31,
                                                      Encoding encoding = null
                                                      )
        {
            if (formatter is null)
            {
                formatter = new CompactJsonFormatter();
            }

            return(configuration.Async(
                       asyncConfiguration => asyncConfiguration.Map(AppDomainId, (_, mapConfiguration) =>
                                                                    mapConfiguration.File(
                                                                        formatter,
                                                                        path,
                                                                        restrictedToMinimumLevel,
                                                                        fileSizeLimitBytes,
                                                                        levelSwitch,
                                                                        buffered: true,
                                                                        shared: false,
                                                                        flushToDiskInterval,
                                                                        rollingInterval,
                                                                        rollOnFileSizeLimit,
                                                                        retainedFileCountLimit,
                                                                        encoding,
                                                                        null),
                                                                    sinkMapCountLimit: 0)
                       ));
        }
Пример #13
0
        private static void ConfigureLogger(HostBuilderContext hostBuilderContext, LoggerConfiguration loggerConfiguration)
        {
            if (Debugger.IsAttached)
            {
                // allow serilog warnings to show when running in debug
                // see https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics
                // if you're having issues with serilog in some environments (sandbox/uat/prod) consider enabling this
                Serilog.Debugging.SelfLog.Enable(Console.Error);
            }

            // setup your serilog logger configuration
            // the configuration below is just an example, you should configure it as it fits your use case
            var jsonValueFormatter = new JsonValueFormatter(typeTagName: null); //passing null removes $type from being added to all serialized objects
            var jsonFormatter      = new CompactJsonFormatter(jsonValueFormatter);
            var exceptionDetailsDeconstructorers = new DestructuringOptionsBuilder().WithDefaultDestructurers();

            loggerConfiguration.ReadFrom.Configuration(hostBuilderContext.Configuration)
            .Destructure.UsingAttributes()
            .Enrich.FromLogContext()
            .Enrich.WithExceptionDetails(exceptionDetailsDeconstructorers)
            .Enrich.WithMachineName()
            .Enrich.WithEnvironmentName()
            .WriteTo.Console(jsonFormatter);
        }
        private static LoggerConfiguration configFromWebApiConfig(LoggerConfiguration loggerConfiguration, WebApiConfig apiConfig)
        {
            apiConfig = apiConfig ?? new WebApiConfig();

            loggerConfiguration.MinimumLevel.ControlledBy(apiConfig.InnerSerilogLevels.AppLogLevel)
            .MinimumLevel.Override("Microsoft", apiConfig.InnerSerilogLevels.SystemLogLevel)
            .MinimumLevel.Override("System", apiConfig.InnerSerilogLevels.SystemLogLevel)
            .MinimumLevel.Override("Microsoft.AspNetCore", apiConfig.InnerSerilogLevels.AppLogLevel)
            .MinimumLevel.Override("Microsoft.Hosting", apiConfig.InnerSerilogLevels.AppLogLevel)
            .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", apiConfig.InnerSerilogLevels.EFCoreCommandLevel)
            .MinimumLevel.Override("System.Net.Http.HttpClient", apiConfig.InnerSerilogLevels.AppLogLevel)
            .MinimumLevel.Override("ServerRequest.Logger", apiConfig.InnerSerilogLevels.ServerRequestLevel)
            .MinimumLevel.Override("ClientRequest.Logger", apiConfig.InnerSerilogLevels.ClientRequestLevel)
            .Destructure.ToMaximumStringLength(apiConfig.MaxLogLength);

            loggerConfiguration = loggerConfiguration.Enrich.FromLogContext();
            if (apiConfig.ConsoleJsonLog)
            {
                loggerConfiguration = loggerConfiguration.WriteTo.Console(new CompactJsonFormatter(apiConfig));
            }
            else if (apiConfig.ConsoleLog)
            {
                loggerConfiguration = loggerConfiguration.WriteTo.Console();
            }
            else
            {
                loggerConfiguration = loggerConfiguration.WriteTo.Console(Serilog.Events.LogEventLevel.Warning);
            }
            if (apiConfig.FileLog || apiConfig.FileJsonLog)
            {
                string path = apiConfig.GetLogPath();

                string pathTemplate = apiConfig.LogPathTemplate;
                if (string.IsNullOrEmpty(pathTemplate))
                {
                    pathTemplate = LogPathTemplates.DayFolderAndLoggerNameFile;
                }

                Serilog.Formatting.Display.MessageTemplateTextFormatter pathFormatter = new Serilog.Formatting.Display.MessageTemplateTextFormatter(pathTemplate);

                ArchiveHooks archiveHooks = new ArchiveHooks(CompressionLevel.Fastest);
                string       template     = apiConfig.LogTemplate;
                if (string.IsNullOrEmpty(template))
                {
                    template = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{NewLine}{Exception}";
                }
                bool keySelector(Serilog.Events.LogEvent logEvent, out LogPathAndTimeKey key)
                {
                    StringWriter  sw = new StringWriter();
                    StringBuilder sb = new StringBuilder();

                    pathFormatter.Format(logEvent, sw);
                    sw.Flush();
                    string dp = sw.GetStringBuilder().ToString();
                    string p  = Path.Combine(path, dp);

                    key = new LogPathAndTimeKey()
                    {
                        Path = p,
                        Time = logEvent.Timestamp.Date
                    };
                    return(true);
                };

                if (apiConfig.FileJsonLog)
                {
                    var formatter = new CompactJsonFormatter(apiConfig);
                    loggerConfiguration = loggerConfiguration
                                          .WriteTo.MapCondition <LogPathAndTimeKey>(keySelector, (path, lc) =>
                    {
                        lc.Async(lc => lc.File(
                                     formatter,
                                     path.Path,
                                     rollingInterval: RollingInterval.Infinite,
                                     rollOnFileSizeLimit: true,
                                     fileSizeLimitBytes: apiConfig.MaxLogFileSize,
                                     retainedFileCountLimit: apiConfig.RetainedFileCount,
                                     hooks: archiveHooks));
                    }, key =>
                    {
                        // 自动Dispose前一天的日志
                        DateTimeOffset now = DateTimeOffset.Now.Date;
                        if (now > key.Time.Date)
                        {
                            return(true);
                        }
                        return(false);
                    });
                }
                else
                {
                    loggerConfiguration = loggerConfiguration
                                          .WriteTo.MapCondition <LogPathAndTimeKey>(keySelector, (path, lc) =>
                    {
                        lc.Async(lc => lc.File(
                                     path.Path,
                                     rollingInterval: RollingInterval.Infinite,
                                     rollOnFileSizeLimit: true,
                                     fileSizeLimitBytes: apiConfig.MaxLogFileSize,
                                     retainedFileCountLimit: apiConfig.RetainedFileCount,
                                     hooks: archiveHooks,
                                     outputTemplate: template));
                    }, key =>
                    {
                        // 自动Dispose前一天的日志
                        DateTimeOffset now = DateTimeOffset.Now.Date;
                        if (now > key.Time.Date)
                        {
                            return(true);
                        }
                        return(false);
                    });
                }
            }

            return(loggerConfiguration);
        }
Пример #15
0
        public static SyncResult Sync(ISimpleJsonRest web, StandardNoteConnection conn, APIResultAuthorize authToken, StandardNoteConfig cfg, StandardNoteData dat, List <StandardFileNote> allNotes, List <StandardFileNote> notesUpload, List <StandardFileNote> notesDelete, List <StandardFileTag> tagsDelete)
        {
            APIBodySync d = new APIBodySync();

            d.cursor_token = null;
            d.sync_token   = string.IsNullOrWhiteSpace(dat.SyncToken) ? null : dat.SyncToken;
            d.limit        = 150;
            d.items        = new List <APIBodyItem>();

            var items_raw = new List <APIRawBodyItem>();

            var allTags = dat.Tags.ToList();

            // Upload new notes
            foreach (var mvNote in notesUpload)
            {
                PrepareNoteForUpload(web, d, ref items_raw, mvNote, allTags, authToken, cfg, false);
            }

            // Delete deleted notes
            foreach (var rmNote in notesDelete)
            {
                PrepareNoteForUpload(web, d, ref items_raw, rmNote, allTags, authToken, cfg, true);
            }

            // Update references on tags (from changed notes)
            foreach (var upTag in GetTagsInNeedOfUpdate(dat.Tags, notesUpload, notesDelete))
            {
                PrepareTagForUpload(web, d, ref items_raw, upTag, authToken, false);
            }

            // Remove unused tags
            if (cfg.RemEmptyTags)
            {
                foreach (var rmTag in tagsDelete)
                {
                    PrepareTagForUpload(web, d, ref items_raw, rmTag, authToken, true);
                }
            }

            Logger.Debug(
                StandardNotePlugin.Name,
                $"Perform sync request ({items_raw.Count} items send)",
                "Sent Items (unencrypted):\n\n" + string.Join("\n", items_raw.Select(i => $"{{\n  content_type = {i.content_type}\n  uuid         = {i.uuid}\n  created_at   = {i.created_at}\n  deleted      = {i.deleted}\n  content      =\n{CompactJsonFormatter.FormatJSON(i.content, 2, 1)}\n}}")));

            var result = GetCursorResult(web, dat, d);

            var syncresult = new SyncResult();

            syncresult.retrieved_tags = result
                                        .retrieved_items
                                        .Where(p => p.content_type.ToLower() == "tag")
                                        .Where(p => !p.deleted)
                                        .Select(n => CreateTag(web, n, authToken))
                                        .ToList();

            syncresult.deleted_tags = result
                                      .retrieved_items
                                      .Where(p => p.content_type.ToLower() == "tag")
                                      .Where(p => p.deleted)
                                      .Select(n => CreateTag(web, n, authToken))
                                      .ToList();

            syncresult.saved_tags = result
                                    .saved_items
                                    .Where(p => p.content_type.ToLower() == "tag")
                                    .Select(n => CreateTag(web, n, authToken))
                                    .ToList();

            syncresult.unsaved_tags = result
                                      .unsaved
                                      .Where(p => p.item.content_type.ToLower() == "tag")
                                      .Select(n => CreateTag(web, n.item, authToken))
                                      .ToList();

            dat.UpdateTags(syncresult.retrieved_tags, syncresult.saved_tags, syncresult.unsaved_tags, syncresult.deleted_tags);

            syncresult.retrieved_notes = result
                                         .retrieved_items
                                         .Where(p => p.content_type.ToLower() == "note")
                                         .Where(p => !p.deleted)
                                         .Select(n => CreateNote(web, conn, n, authToken, cfg, dat))
                                         .ToList();

            syncresult.deleted_notes = result
                                       .retrieved_items
                                       .Where(p => p.content_type.ToLower() == "note")
                                       .Where(p => p.deleted)
                                       .Select(n => CreateNote(web, conn, n, authToken, cfg, dat))
                                       .ToList();

            syncresult.saved_notes = result
                                     .saved_items
                                     .Where(p => p.content_type.ToLower() == "note")
                                     .Select(n => CreateNote(web, conn, n, authToken, cfg, dat))
                                     .ToList();

            syncresult.conflict_notes = result
                                        .unsaved
                                        .Where(p => p.item.content_type.ToLower() == "note")
                                        .Where(p => p.error.tag == "sync_conflict")
                                        .Select(n => CreateNote(web, conn, n.item, authToken, cfg, dat))
                                        .ToList();

            syncresult.error_notes = result
                                     .unsaved
                                     .Where(p => p.item.content_type.ToLower() == "note")
                                     .Where(p => p.error.tag != "sync_conflict")
                                     .Select(n => CreateNote(web, conn, n.item, authToken, cfg, dat))
                                     .ToList();

            syncresult.retrieved_notes.AddRange(GetMissingNoteUpdates(syncresult.retrieved_tags.Concat(syncresult.saved_tags), dat.Tags, allNotes, syncresult.retrieved_notes));

            return(syncresult);
        }