diff --git a/README.md b/README.md index 5795674c5..024054c08 100644 --- a/README.md +++ b/README.md @@ -1035,3 +1035,4 @@ As the new Tesla Fleet API requires a domain and external Token creation from ve - Your access code is used to get the access token from Tesla (Note: the token itself is only stored locally in your TSC installation. It is only transferred via my server, but the token only exists in memory on the server itself. It is not stored in a database or log file) - Your installation ID (GUID) is at the bottom of the page. Do not post this GUID in public forums, as it is used to deliver the Tesla access token to your installation. Note: There is only a five-minute time window between requesting and providing the token using the installation ID. After these 5 minutes, all requests are blocked.) - Your installed version. +- Error and warning logs diff --git a/TeslaSolarCharger.Model/EntityFramework/DbConnectionStringHelper.cs b/TeslaSolarCharger.Model/EntityFramework/DbConnectionStringHelper.cs index dd097870d..26278b9f3 100644 --- a/TeslaSolarCharger.Model/EntityFramework/DbConnectionStringHelper.cs +++ b/TeslaSolarCharger.Model/EntityFramework/DbConnectionStringHelper.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using TeslaSolarCharger.Model.Contracts; using TeslaSolarCharger.Shared.Contracts; @@ -31,7 +31,8 @@ public string GetTeslaMateConnectionString() public string GetTeslaSolarChargerDbPath() { _logger.LogTrace("{method}()", nameof(GetTeslaSolarChargerDbPath)); - var connectionString = $"Data Source={_configurationWrapper.SqliteFileFullName()};Pooling=False"; + var filePath = _configurationWrapper.SqliteFileFullName(); + var connectionString = $"Data Source={filePath};Pooling=False"; return connectionString; } } diff --git a/TeslaSolarCharger/Server/Program.cs b/TeslaSolarCharger/Server/Program.cs index 538bbef51..4df189466 100644 --- a/TeslaSolarCharger/Server/Program.cs +++ b/TeslaSolarCharger/Server/Program.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using Serilog; +using Serilog.Context; using TeslaSolarCharger.GridPriceProvider; using TeslaSolarCharger.Model.Contracts; using TeslaSolarCharger.Server; @@ -44,6 +45,11 @@ var app = builder.Build(); +Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(app.Services.GetRequiredService()) + .Enrich.FromLogContext() + .CreateLogger(); + //Do nothing before these lines as BaseConfig.json is created here. This results in breaking new installations! var logger = app.Services.GetRequiredService>(); @@ -56,16 +62,21 @@ await baseConfigurationConverter.ConvertAllEnvironmentVariables().ConfigureAwait(false); await baseConfigurationConverter.ConvertBaseConfigToV1_0().ConfigureAwait(false); - var coreService = app.Services.GetRequiredService(); - coreService.LogVersion(); //Do nothing before these lines as database is created here. var teslaSolarChargerContext = app.Services.GetRequiredService(); await teslaSolarChargerContext.Database.MigrateAsync().ConfigureAwait(false); + var tscConfigurationService = app.Services.GetRequiredService(); + var installationId = await tscConfigurationService.GetInstallationId().ConfigureAwait(false); var backendApiService = app.Services.GetRequiredService(); + var version = await backendApiService.GetCurrentVersion().ConfigureAwait(false); + LogContext.PushProperty("InstallationId", installationId); + LogContext.PushProperty("Version", version); + await backendApiService.PostInstallationInformation("Startup").ConfigureAwait(false); + var coreService = app.Services.GetRequiredService(); await coreService.BackupDatabaseIfNeeded().ConfigureAwait(false); var life = app.Services.GetRequiredService(); @@ -74,10 +85,6 @@ coreService.KillAllServices().GetAwaiter().GetResult(); }); - var tscConfigurationService = app.Services.GetRequiredService(); - var installationId = await tscConfigurationService.GetInstallationId().ConfigureAwait(false); - logger.LogInformation("Installation Id: {installationId}", installationId); - var chargingCostService = app.Services.GetRequiredService(); await chargingCostService.DeleteDuplicatedHandleCharges().ConfigureAwait(false); diff --git a/TeslaSolarCharger/Server/Services/ConfigJsonService.cs b/TeslaSolarCharger/Server/Services/ConfigJsonService.cs index 4da74150d..282eb3696 100644 --- a/TeslaSolarCharger/Server/Services/ConfigJsonService.cs +++ b/TeslaSolarCharger/Server/Services/ConfigJsonService.cs @@ -243,7 +243,7 @@ public async Task AddCarsToTscDatabase() var carsToManage = _settings.Cars.Where(c => c.CarConfiguration.ShouldBeManaged == true).ToList(); foreach (var car in carsToManage) { - var databaseCar = await _teslaSolarChargerContext.Cars.FirstOrDefaultAsync(c => c.Id == car.Id).ConfigureAwait(false); + var databaseCar = await _teslaSolarChargerContext.Cars.FirstOrDefaultAsync(c => c.TeslaMateCarId == car.Id).ConfigureAwait(false); if (databaseCar != default) { continue; @@ -251,7 +251,6 @@ public async Task AddCarsToTscDatabase() databaseCar = new Model.Entities.TeslaSolarCharger.Car() { - Id = car.Id, TeslaMateCarId = car.Id, }; _teslaSolarChargerContext.Cars.Add(databaseCar); diff --git a/TeslaSolarCharger/Server/Services/Contracts/IBackendApiService.cs b/TeslaSolarCharger/Server/Services/Contracts/IBackendApiService.cs index e8e79b497..b53cc9d6b 100644 --- a/TeslaSolarCharger/Server/Services/Contracts/IBackendApiService.cs +++ b/TeslaSolarCharger/Server/Services/Contracts/IBackendApiService.cs @@ -7,4 +7,5 @@ public interface IBackendApiService Task> StartTeslaOAuth(string locale, string baseUrl); Task PostInstallationInformation(string reason); Task PostErrorInformation(string source, string methodName, string message, string? stackTrace = null); + Task GetCurrentVersion(); } diff --git a/TeslaSolarCharger/Server/Services/PvValueService.cs b/TeslaSolarCharger/Server/Services/PvValueService.cs index 06f64d63c..aa1f565f7 100644 --- a/TeslaSolarCharger/Server/Services/PvValueService.cs +++ b/TeslaSolarCharger/Server/Services/PvValueService.cs @@ -98,7 +98,7 @@ public async Task UpdatePvValues() patternType, xmlAttributeHeaderName, xmlAttributeHeaderValue, xmlAttributeValueName).ConfigureAwait(false); if (inverterPower < 0) { - _logger.LogWarning("Inverterpower is below 0: {inverterPower}, using -1 for further purposes", inverterPower); + _logger.LogInformation("Inverterpower is below 0: {inverterPower}, using -1 for further purposes", inverterPower); inverterPower = -1; } _settings.InverterPower = inverterPower; diff --git a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs index 4a53c8ef4..a569f567c 100644 --- a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs @@ -75,7 +75,7 @@ public class TeslaFleetApiService( private DtoFleetApiRequest VehicleRequest => new() { - RequestUrl = $"", + RequestUrl = "", NeedsProxy = false, }; diff --git a/TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj b/TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj index 92f4f511a..1e6ee379a 100644 --- a/TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj +++ b/TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj @@ -54,6 +54,7 @@ + diff --git a/TeslaSolarCharger/Server/appsettings.Development.json b/TeslaSolarCharger/Server/appsettings.Development.json index 9fabe6703..f1ebbf654 100644 --- a/TeslaSolarCharger/Server/appsettings.Development.json +++ b/TeslaSolarCharger/Server/appsettings.Development.json @@ -1,7 +1,8 @@ { "Serilog": { "Using": [ - "Serilog.Sinks.Console" + "Serilog.Sinks.Console", + "Serilog.Sinks.Seq" ], "MinimumLevel": { "Default": "Verbose", @@ -19,6 +20,14 @@ "Args": { "outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}" } + }, + { + "Name": "Seq", + "Args": { + "serverUrl": "https://www.teslasolarcharger.de/seq-logging", + "restrictedToMinimumLevel": "Warning", + "outputTemplate": "[{Timestamp:dd-MMM-yyyy HH:mm:ss.fff} {Level:u3} {SourceContext} {InstallationId} {Version}] {Message:lj}{NewLine}{Exception}" + } } ], "Enrich": [ diff --git a/TeslaSolarCharger/Server/appsettings.json b/TeslaSolarCharger/Server/appsettings.json index d3bf5944f..872107bc0 100644 --- a/TeslaSolarCharger/Server/appsettings.json +++ b/TeslaSolarCharger/Server/appsettings.json @@ -1,7 +1,8 @@ { "Serilog": { "Using": [ - "Serilog.Sinks.Console" + "Serilog.Sinks.Console", + "Serilog.Sinks.Seq" ], "MinimumLevel": { "Default": "Debug", @@ -22,17 +23,15 @@ "Args": { "outputTemplate": "[{Timestamp:dd-MMM-yyyy HH:mm:ss.fff} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}" } - } //, - //{ - // "Name": "File", - // "Args": { - // "outputTemplate": "[{Timestamp:dd-MMM-yyyy HH:mm:ss.fff} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}", - // "path": "configs/log.txt", - // "rollingIntervall": "Day", - // "rollOnFileSizeLimit": true, - // "retainedFileCountLimit": 7 - // } - //} + }, + { + "Name": "Seq", + "Args": { + "serverUrl": "https://www.teslasolarcharger.de/seq-logging", + "restrictedToMinimumLevel": "Warning", + "outputTemplate": "[{Timestamp:dd-MMM-yyyy HH:mm:ss.fff} {Level:u3} {SourceContext} {InstallationId} {Version}] {Message:lj}{NewLine}{Exception}" + } + } ], "Enrich": [ "FromLogContext"