web nlog, adding DB context
This commit is contained in:
parent
a85cae8a15
commit
c81f7c08f3
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
@ -12,7 +12,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.11" >
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
|
45
Selector.Model/SelectorContext.cs
Normal file
45
Selector.Model/SelectorContext.cs
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Selector.Model
|
||||
{
|
||||
|
||||
public class SelectorContext : DbContext
|
||||
{
|
||||
public DbSet<Watcher> Watcher { get; set; }
|
||||
|
||||
public SelectorContext(DbContextOptions<SelectorContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<SelectorContext>
|
||||
{
|
||||
public SelectorContext CreateDbContext(string[] args)
|
||||
{
|
||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json")
|
||||
.Build();
|
||||
|
||||
var builder = new DbContextOptionsBuilder<SelectorContext>();
|
||||
builder.UseNpgsql(configuration.GetConnectionString("Default"));
|
||||
|
||||
return new SelectorContext(builder.Options);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Selector.Model
|
||||
{
|
||||
public class Watcher
|
||||
{
|
||||
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Selector.Model;
|
||||
|
||||
namespace Selector.Web.Controller {
|
||||
|
||||
@ -7,10 +12,18 @@ namespace Selector.Web.Controller {
|
||||
[Route("api/[controller]")]
|
||||
public class TestController {
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<string> Get()
|
||||
private readonly SelectorContext db;
|
||||
|
||||
public TestController(SelectorContext context)
|
||||
{
|
||||
return "Hello World!";
|
||||
db = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Watcher>>> Get()
|
||||
{
|
||||
// var watchers = ;
|
||||
return await db.Watcher.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -8,3 +8,7 @@
|
||||
<h1 class="display-4">Welcome</h1>
|
||||
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script type="module" src="~/js/index.bundle.js"></script>
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>@ViewData["Title"] - Selector</title>
|
||||
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
@ -40,8 +40,6 @@
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script type="module" src="~/js/app.bundle.js"></script>
|
||||
|
||||
@await RenderSectionAsync("Scripts", required: false)
|
||||
</body>
|
||||
</html>
|
||||
|
@ -7,6 +7,8 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
namespace Selector.Web
|
||||
{
|
||||
public class Program
|
||||
@ -21,6 +23,11 @@ namespace Selector.Web
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
})
|
||||
.ConfigureLogging((context, builder) => {
|
||||
builder.ClearProviders();
|
||||
builder.SetMinimumLevel(LogLevel.Trace);
|
||||
builder.AddNLog(context.Configuration);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -12,16 +12,33 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.4.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
|
||||
<PackageReference Include="NLog" Version="4.7.11" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.14.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.Development.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="nlog.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -9,6 +9,10 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using Selector.Model;
|
||||
|
||||
namespace Selector.Web
|
||||
{
|
||||
public class Startup
|
||||
@ -23,8 +27,12 @@ namespace Selector.Web
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddRazorPages();
|
||||
services.AddRazorPages().AddRazorRuntimeCompilation();
|
||||
services.AddControllers();
|
||||
|
||||
services.AddDbContext<SelectorContext>(options =>
|
||||
options.UseNpgsql(Configuration.GetConnectionString("Default"))
|
||||
);
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
47
Selector.Web/nlog.config
Normal file
47
Selector.Web/nlog.config
Normal file
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
autoReload="true"
|
||||
throwConfigExceptions="true"
|
||||
internalLogFile=".\log\selector.nlog.log"
|
||||
internalLogLevel="Info" >
|
||||
|
||||
<extensions>
|
||||
<add assembly="NLog.Web.AspNetCore"/>
|
||||
</extensions>
|
||||
|
||||
<variable name="format"
|
||||
value="${longdate}|${level:uppercase=true}|${callsite}:${callsite-linenumber}|${message}${onexception:inner=${newline}}${exception:format=tostring,data:exceptionDataSeparator=\r\n}"/>
|
||||
|
||||
<!-- the targets to write to -->
|
||||
<targets>
|
||||
<!-- write logs to file -->
|
||||
<target xsi:type="File"
|
||||
name="logfile"
|
||||
fileName=".\log\selector-${shortdate}.log"
|
||||
layout="${format}" />
|
||||
<target xsi:type="File"
|
||||
name="tracefile"
|
||||
fileName=".\log\selector.trace-${shortdate}.log"
|
||||
layout="${format}" />
|
||||
<target xsi:type="Console"
|
||||
name="logconsole"
|
||||
layout="${format}" />
|
||||
</targets>
|
||||
|
||||
<!-- rules to map from logger name to target -->
|
||||
<rules>
|
||||
<logger name="*" minlevel="Trace" writeTo="tracefile" />
|
||||
<logger name="Selector.*" minlevel="Debug" writeTo="logconsole" />
|
||||
|
||||
<!--Output hosting lifetime messages to console target for faster startup detection -->
|
||||
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="logconsole" final="true" />
|
||||
|
||||
<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
|
||||
<logger name="Microsoft.*" maxlevel="Info" final="true" />
|
||||
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
|
||||
|
||||
<logger name="*" minlevel="Debug" writeTo="logfile" />
|
||||
</rules>
|
||||
</nlog>
|
@ -3,7 +3,7 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
|
||||
|
||||
module.exports = {
|
||||
entry: {
|
||||
app: './scripts/index.ts',
|
||||
index: './scripts/index.ts',
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
|
Loading…
Reference in New Issue
Block a user