public AzureMonitorLogger(EnvironmentSetting environment, string harvesterId) { _issueReporter = YouTrackIssueConnector.GetInstance(environment); // Get the Instrumentation Key for Azure from an environment variable. string environmentVarName = "BloomHarvesterAzureAppInsightsKeyDev"; if (environment == EnvironmentSetting.Test) { environmentVarName = "BloomHarvesterAzureAppInsightsKeyTest"; } else if (environment == EnvironmentSetting.Prod) { environmentVarName = "BloomHarvesterAzureAppInsightsKeyProd"; } string instrumentationKey = Environment.GetEnvironmentVariable(environmentVarName); Debug.Assert(!String.IsNullOrWhiteSpace(instrumentationKey), "Azure Instrumentation Key is invalid. Azure logging probably won't work."); try { Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey; } catch (ArgumentNullException e) { _issueReporter.ReportException(e, $"InstrumentationKey: {instrumentationKey ?? "null"}.\nenvironmentVarName: {environmentVarName}", null); } _telemetry.Context.User.Id = "BloomHarvester " + harvesterId; _telemetry.Context.Session.Id = Guid.NewGuid().ToString(); _telemetry.Context.Device.OperatingSystem = Environment.OSVersion.ToString(); string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BloomHarvester", "log.txt"); Console.Out.WriteLine("Creating log file at: " + logFilePath); try { if (File.Exists(logFilePath)) { // Check if the file is too big (~10 MB) if (new FileInfo(logFilePath).Length > 10000000) { var oldPath = logFilePath + "-OLD"; // Preserve one previous log file for debugging help, and start over with // an empty log file. // (The data is in Azure too anyway, but having it local may speed things up.) if (RobustFile.Exists(oldPath)) { RobustFile.Delete(oldPath); } RobustFile.Move(logFilePath, oldPath); } } } catch { // Doesn't matter if there are any errors } try { _fileLogger = new FileLogger(logFilePath); } catch { // That's unfortunate that creating the logger failed, but I don't really want to throw an exception since the file logger isn't even the main purpose of this calss. // Let's just replace it with something to get it to be quiet. _fileLogger = new NullLogger(); } }