示例#1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Application"/> class.
        /// </summary>
        public Application(CancellationToken cancellationToken)
        {
            _cancellationToken = cancellationToken;
            _consoleLog        = new ConsoleLog(Severity.Warning);
            _logger            = new Logger(true, _consoleLog);
            _fileSystem        = new PassThroughFileSystem(_logger);
            _tracer            = new Tracer(nameof(Application));

            var kustoConnectionString = Environment.GetEnvironmentVariable(KustoConnectionStringEnvVarName);

            _kustoUploader = string.IsNullOrWhiteSpace(kustoConnectionString)
                ? null
                : new KustoUploader
                             (
                kustoConnectionString,
                database: KustoDatabase,
                table: KustoTable,
                deleteFilesOnSuccess: true,
                checkForIngestionErrors: true,
                log: _consoleLog
                             );
        }
示例#2
0
        private void EnableRemoteTelemetryIfNeeded(string logFilePath)
        {
            if (!_enableRemoteTelemetry)
            {
                return;
            }

            var kustoConnectionString = Environment.GetEnvironmentVariable(KustoConnectionStringEnvVarName);

            if (string.IsNullOrWhiteSpace(kustoConnectionString))
            {
                _logger.Warning
                (
                    "Remote telemetry flag is enabled but no Kusto connection string was found in environment variable '{0}'",
                    KustoConnectionStringEnvVarName
                );
                return;
            }

            _csvFileLog = new CsvFileLog
                          (
                logFilePath: logFilePath + TmpCsvLogFileExt,
                serviceName: ServiceName,
                schema: KustoTableCsvSchema,
                renderConstColums: false,
                severity: _fileLogSeverity,
                maxFileSize: _csvLogMaxFileSize
                          );

            var indexedColumns = _csvFileLog.FileSchema.Select((col, idx) => new CsvColumnMapping {
                ColumnName = col.ToString(), Ordinal = idx
            });
            var constColumns = _csvFileLog.ConstSchema.Select(col => new CsvColumnMapping {
                ColumnName = col.ToString(), ConstValue = _csvFileLog.RenderConstColumn(col)
            });
            var csvMapping = indexedColumns.Concat(constColumns).ToArray();

            var csvMappingStr = string.Join("", csvMapping.Select(col => $"{Environment.NewLine}  Name: '{col.ColumnName}', ConstValue: '{col.ConstValue}', Ordinal: {col.Ordinal}"));

            _logger.Always("Using csv mapping:{0}", csvMappingStr);

            _kustoUploader = new KustoUploader
                             (
                kustoConnectionString,
                database: KustoDatabase,
                table: KustoTable,
                csvMapping: csvMapping,
                deleteFilesOnSuccess: true,
                checkForIngestionErrors: true,
                log: _consoleLog
                             );

            // Every time a log file written to disk and closed, we rename it and upload it to Kusto.
            // The last log file will be produced when _csvFileLog is disposed, so _kustUploader better
            // not be disposed before _csvFileLog.
            _csvFileLog.OnLogFileProduced += (path) =>
            {
                string newPath = Path.ChangeExtension(path, CsvLogFileExt);
                File.Move(path, newPath);
                _kustoUploader.PostFileForUpload(newPath, _csvFileLog.BuildId);
            };

            _logger.AddLog(_csvFileLog);
            _logger.Always("Remote telemetry enabled");
        }