Lesson 1 with .net .gitignore and clean repo

This commit is contained in:
2025-11-28 15:11:41 -06:00
commit 3dc91c2fe8
8 changed files with 291 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace NetPulse.Core;
public class EngineLoop : BackgroundService
{
private readonly ILogger<EngineLoop> _logger;
private readonly TimeSpan _interval = TimeSpan.FromSeconds(2);
public EngineLoop(ILogger<EngineLoop> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("EngineLoop started.");
while(!stoppingToken.IsCancellationRequested)
{
try
{
// ===== Core Heartbeat =====
_logger.LogInformation("Hearbeat at {time}", DateTime.UtcNow);
// Future: metrics Collection
// Future: dispatch events
// Future: run modules
await Task.Delay(_interval, stoppingToken);
}
catch (TaskCanceledException)
{
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected Engine error.");
}
}
_logger.LogInformation("EngineLoop Stopping");
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-NetPulse.Core-d06ed89e-b585-4482-b09d-8e11e4e6f936</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.9" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,33 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
namespace NetPulse.Core;
public class Program
{
public static async Task Main(string[] args)
{
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddSimpleConsole(options =>
{
options.TimestampFormat = "HH:MM:SS";
options.IncludeScopes = true;
});
})
.ConfigureServices((context, services) =>
{
services.AddHostedService<EngineLoop>();
})
.Build();
await host.RunAsync();
}
}

View File

@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"NetPulse.Core": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}