added logging
This commit is contained in:
parent
39ca7cba3b
commit
ee92564bf2
|
|
@ -6,10 +6,10 @@ using ProjectGrid.Models;
|
||||||
|
|
||||||
namespace ProjectGrid.Data
|
namespace ProjectGrid.Data
|
||||||
{
|
{
|
||||||
class DataAccessContex : DbContext
|
public class DataAccessContext : DbContext
|
||||||
{
|
{
|
||||||
|
|
||||||
public DataAccessContex(DbContextOptions<DataAccessContex> options) { }
|
public DataAccessContext(DbContextOptions<DataAccessContext> options) { }
|
||||||
|
|
||||||
public DbSet<UserModel> Users;
|
public DbSet<UserModel> Users;
|
||||||
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
using ProjectGrid.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace ProjectGrid.Data
|
||||||
|
{
|
||||||
|
public static class DbInitializer
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void Initialize(DataAccessContext context)
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
// Look for any UserModels.
|
||||||
|
if (context.Users.Any())
|
||||||
|
{
|
||||||
|
return; // DB has been seeded
|
||||||
|
}
|
||||||
|
|
||||||
|
var users = new UserModel[]
|
||||||
|
{
|
||||||
|
new UserModel{firstName="Carson",lastName="Alexander",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Meredith",lastName="Alonso",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Arturo",lastName="Anand",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Gytis",lastName="Barzdukas",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Yan",lastName="Li",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Peggy",lastName="Justice",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Laura",lastName="Norman",email="test@abc.com"},
|
||||||
|
new UserModel{firstName="Nino",lastName="Olivetto",email="test@abc.com"}
|
||||||
|
};
|
||||||
|
foreach (UserModel s in users)
|
||||||
|
{
|
||||||
|
context.Users.Add(s);
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
Program.cs
29
Program.cs
|
|
@ -1,3 +1,5 @@
|
||||||
|
using ProjectGrid.Data;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -11,13 +13,38 @@ namespace ProjectGrid
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static ILogger _logger;
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
CreateHostBuilder(args).Build().Run();
|
var host = CreateHostBuilder(args).Build();
|
||||||
|
|
||||||
|
using (var scope = host.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
_logger = services.GetRequiredService<ILogger<Program>>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var context = services.GetRequiredService<DataAccessContext>();
|
||||||
|
DbInitializer.Initialize(context);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
_logger.LogError(ex, "An error occurred while seeding the database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
host.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
Host.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureLogging(loggingBuilder =>
|
||||||
|
{
|
||||||
|
loggingBuilder.ClearProviders();
|
||||||
|
loggingBuilder.AddDebug().AddEventLog();
|
||||||
|
})
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
{
|
{
|
||||||
webBuilder.UseStartup<Startup>();
|
webBuilder.UseStartup<Startup>();
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
31
Startup.cs
31
Startup.cs
|
|
@ -1,15 +1,14 @@
|
||||||
using System;
|
using Microsoft.ApplicationInsights.Channel;
|
||||||
using System.Collections.Generic;
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
using System.Linq;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Threading.Tasks;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Logging.ApplicationInsights;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.HttpsPolicy;
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ProjectGrid.Data;
|
using ProjectGrid.Data;
|
||||||
|
|
@ -18,22 +17,21 @@ namespace ProjectGrid
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public Startup(IConfiguration configuration)
|
ILogger _logger;
|
||||||
{
|
|
||||||
Configuration = configuration;
|
public Startup() { }
|
||||||
}
|
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
var connectionString = Configuration.GetConnectionString("DefaultConnection");
|
||||||
services.AddDbContext<DataAccessContex>(options =>
|
services.AddDbContext<DataAccessContext>(options => options.UseSqlServer(connectionString));
|
||||||
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
|
//_logger.LogInformation($"DataAccessContext registered with {connectionString}");
|
||||||
|
|
||||||
// TODO: for Testing
|
// TODO: for Testing
|
||||||
services.AddDatabaseDeveloperPageExceptionFilter();
|
//services.AddDatabaseDeveloperPageExceptionFilter();
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddSwaggerGen(c =>
|
services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
|
|
@ -42,8 +40,9 @@ namespace ProjectGrid
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
|
||||||
{
|
{
|
||||||
|
_logger = logger;
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue