diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromProgram/LoggingFromProgram.csproj b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/LoggingFromProgram.csproj new file mode 100644 index 0000000000..4a73db9de5 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/LoggingFromProgram.csproj @@ -0,0 +1,14 @@ + + + + net7.0 + enable + enable + + + + + + + + diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Program.cs b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Program.cs new file mode 100644 index 0000000000..cb9f214bef --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Program.cs @@ -0,0 +1,35 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +app.Logger.LogInformation("This is an informational log from Program.cs"); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapGet("/TestLog", async context => +{ + app.Logger.LogInformation("Testing logging from Minimal API endpoint in Program.cs"); + await context.Response.WriteAsync("Testing"); +}); + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); + +public partial class Program { } diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Properties/launchSettings.json b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Properties/launchSettings.json new file mode 100644 index 0000000000..d3d1d9c8ca --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:36487", + "sslPort": 44332 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5117", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7211;http://localhost:5117", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.Development.json b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.json b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.json new file mode 100644 index 0000000000..10f68b8c8b --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromProgram/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup.sln b/dotnet-logging/LoggingFromStartup/LoggingFromStartup.sln new file mode 100644 index 0000000000..99d953f05c --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33424.131 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggingFromProgram", "LoggingFromProgram\LoggingFromProgram.csproj", "{92B84893-9295-4534-ABCA-1AE84326FAD2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingFromStartup", "LoggingFromStartup\LoggingFromStartup.csproj", "{C2E317F3-661C-40CD-A409-2256C8D55BDE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{A9833E08-E38A-4B48-AD78-506D61320DF3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {92B84893-9295-4534-ABCA-1AE84326FAD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {92B84893-9295-4534-ABCA-1AE84326FAD2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {92B84893-9295-4534-ABCA-1AE84326FAD2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {92B84893-9295-4534-ABCA-1AE84326FAD2}.Release|Any CPU.Build.0 = Release|Any CPU + {C2E317F3-661C-40CD-A409-2256C8D55BDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2E317F3-661C-40CD-A409-2256C8D55BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2E317F3-661C-40CD-A409-2256C8D55BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2E317F3-661C-40CD-A409-2256C8D55BDE}.Release|Any CPU.Build.0 = Release|Any CPU + {A9833E08-E38A-4B48-AD78-506D61320DF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9833E08-E38A-4B48-AD78-506D61320DF3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9833E08-E38A-4B48-AD78-506D61320DF3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9833E08-E38A-4B48-AD78-506D61320DF3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9D4DA780-EC7F-4FB6-B24E-DC27AAF19B50} + EndGlobalSection +EndGlobal diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Controllers/WeatherForecastController.cs b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000000..1bd0d7ff38 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Controllers/WeatherForecastController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LoggingFromStartup.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/LoggingFromStartup.csproj b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/LoggingFromStartup.csproj new file mode 100644 index 0000000000..288471618b --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/LoggingFromStartup.csproj @@ -0,0 +1,11 @@ + + + + net5.0 + + + + + + + diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Program.cs b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Program.cs new file mode 100644 index 0000000000..d406b2c2db --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Program.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace LoggingFromStartup +{ + public class Program + { + public static void Main(string[] args) + { + var host = CreateHostBuilder(args).Build(); + + var logger = host.Services.GetRequiredService>(); + logger.LogInformation("This is an informational log from Main()"); + + host.Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Properties/launchSettings.json b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Properties/launchSettings.json new file mode 100644 index 0000000000..33516eb593 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:8385", + "sslPort": 44330 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "LoggingFromStartup": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Startup.cs b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Startup.cs new file mode 100644 index 0000000000..39eb218178 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/Startup.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Models; + +namespace LoggingFromStartup +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "LoggingFromStartup", Version = "v1" }); + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, + IWebHostEnvironment env, + ILogger logger) + { + logger.LogInformation("This is an informational log from Startup.cs"); + + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "LoggingFromStartup v1")); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/WeatherForecast.cs b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/WeatherForecast.cs new file mode 100644 index 0000000000..14cc568543 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace LoggingFromStartup +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.Development.json b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.Development.json new file mode 100644 index 0000000000..8983e0fc1c --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.json b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.json new file mode 100644 index 0000000000..d9d9a9bff6 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/LoggingFromStartup/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/dotnet-logging/LoggingFromStartup/Tests/LoggingFromProgramTest.cs b/dotnet-logging/LoggingFromStartup/Tests/LoggingFromProgramTest.cs new file mode 100644 index 0000000000..404e2e8f5c --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/Tests/LoggingFromProgramTest.cs @@ -0,0 +1,26 @@ +namespace Tests +{ + public class LoggingFromProgramTest : IClassFixture> + { + + private readonly WebApplicationFactory _factory; + + public LoggingFromProgramTest(WebApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task WhenGettingTestLog_ThenReturnSuccess() + { + // arrange + var client = _factory.CreateClient(); + + // act + var response = await client.GetAsync("TestLog"); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + } +} diff --git a/dotnet-logging/LoggingFromStartup/Tests/LoggingFromStartupTest.cs b/dotnet-logging/LoggingFromStartup/Tests/LoggingFromStartupTest.cs new file mode 100644 index 0000000000..3ea1d9c95c --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/Tests/LoggingFromStartupTest.cs @@ -0,0 +1,26 @@ +namespace Tests +{ + public class LoggingFromStartupTest : IClassFixture> + { + + private readonly WebApplicationFactory _factory; + + public LoggingFromStartupTest(WebApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task WhenGettingWeatherForecast_ThenReturnSuccess() + { + // arrange + var client = _factory.CreateClient(); + + // act + var response = await client.GetAsync("WeatherForecast"); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + } +} \ No newline at end of file diff --git a/dotnet-logging/LoggingFromStartup/Tests/Tests.csproj b/dotnet-logging/LoggingFromStartup/Tests/Tests.csproj new file mode 100644 index 0000000000..1fc50395e8 --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/Tests/Tests.csproj @@ -0,0 +1,30 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/dotnet-logging/LoggingFromStartup/Tests/Usings.cs b/dotnet-logging/LoggingFromStartup/Tests/Usings.cs new file mode 100644 index 0000000000..3676600e7f --- /dev/null +++ b/dotnet-logging/LoggingFromStartup/Tests/Usings.cs @@ -0,0 +1,3 @@ +global using Xunit; +global using Microsoft.AspNetCore.Mvc.Testing; +global using System.Net; \ No newline at end of file