Exemplo n.º 1
0
        public CollectModule(IAppConfigurationManager config, IValidator <LogRecord> logRecordValidator, ILogStore logStore)
        {
            Post["/collect", true] = async(x, ct) => {
                var logrec = this.Bind <LogRecord>(new BindingConfig {
                    BodyOnly = true
                });
                var validationResult = logRecordValidator.Validate(logrec);
                if (!validationResult.IsValid)
                {
                    return("VALIDATION ERROR");
                }
                logrec.ApplicationPath = logrec.ApplicationPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                var app = await config.FindAppAsync(logrec.ApplicationPath);

                if (app == null)
                {
                    app = new Application {
                        IsExcluded = true,
                        Path       = logrec.ApplicationPath
                    };
                    await config.AddOrUpdateAppAsync(app);
                }
                if (!app.IsExcluded)
                {
                    await logStore.AddLogRecordAsync(logrec);

                    await logStore.UpdateApplicationStatusAsync(
                        CreateApplicationStatus(logrec));
                }
                return("OK");
            };
            Post["/collectall", true] = async(x, ct) => {
                var logrecs = this.Bind <LogRecord[]>(new BindingConfig {
                    BodyOnly = true
                });

                var logsToSave = new List <LogRecord>(logrecs.Length);
                foreach (var logrec in logrecs)
                {
                    var validationResult = logRecordValidator.Validate(logrec);
                    if (validationResult.IsValid)
                    {
                        logrec.ApplicationPath = logrec.ApplicationPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                        // add new application to the configuration as excluded (it could be later renamed or unexcluded)
                        var app = await config.FindAppAsync(logrec.ApplicationPath);

                        if (app == null)
                        {
                            app = new Application {
                                IsExcluded = true,
                                Path       = logrec.ApplicationPath
                            };
                            await config.AddOrUpdateAppAsync(app);
                        }
                        if (!app.IsExcluded)
                        {
                            logsToSave.Add(logrec);
                        }
                        else
                        {
                            logger.TraceEvent(TraceEventType.Verbose, 0, "Log record for the application '{0}' was not stored as the application is excluded.");
                        }
                    }
                    else
                    {
                        if (logger.Switch.ShouldTrace(TraceEventType.Warning))
                        {
                            logger.TraceEvent(TraceEventType.Warning, 0, "Validation error(s) occured when saving a logrecord: {0}, errors: {1}",
                                              logrec, validationResult.Errors);
                        }
                    }
                }

                if (logsToSave.Count > 0)
                {
                    await logStore.AddLogRecordsAsync(logsToSave);

                    await logStore.UpdateApplicationStatusesAsync(CreateApplicationStatusesList(logsToSave));
                }

                return("OK");
            };
        }