UseSerilog unknown reference in ConfigureWebHostDefaults - asp.net-core

i followed few tutorials and im stuck building the default dotnet 3.1 with serilog
in program.cs
this line wont build
here is the code
https://github.com/guymalka/serilog-dotnetcore/blob/master/Program.cs
what reference do I need to add here?

First include nuget package reference in your .csproj file:
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
and dotnet restore your project.
After that import the Serilog namespace in your Program.cs file (you could do that in Startup as well):
using Serilog;
That should be enough for you to register Serilog like so:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Note that if you wish to configure other aspects, e.g., your sinks, with Serilog chances are you'll have to include separate packages:
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />

Related

Localhost not found after migrating application from .net 5 to .net 6

I am unable to run my web application project in IIS Express after migrating from .net 5 to .net 6. I get an error stating that "the localhost page can't be found."
From what I've been able to learn, I have gone through the proper steps to migrate between target frameworks. I have changed the target framework to .net 6 in application properties, and I have updated all affected NuGet packages using the NuGet Pkg Manager. No errors are shown when I build the project, and I have ensured Visual Studio is updated to the latest version, 17.2.5 as of this writing.
The IIS Settings in my launchsettings.json file are as follows:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35317",
"sslPort": 44352
}
}
and I do have an Index view return in my home controller. Yet I still get the error that
No webpage was found for the web address: https://localhost:44352/
when I run the application.
I am retaining the "old" hosting model of Startup.cs and Program.cs from the .Net 5 build to save time, as I understand per Microsoft that this is acceptable. No changes have been made to the code in either of those files.
What am I missing?
EDIT: My Program.cs code is below, by request of #adrianMoskal
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
EDIT 2:
Per #adrianMoskal, I updated my Program.cs file to .NET 6 standard. It now looks like this:
var builder = WebApplication.CreateBuilder(args);
// Add DB Contexts and Services here
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Unfortunately, the error persists.
I created a new project to migrate from .net 5 to .net 6, but no problem, it works fine, I will show you the complete steps:
.csproj:
change Version like below:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<!--<TargetFramework>net5.0</TargetFramework>-->
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_2022070802</RootNamespace>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
<ItemGroup>
<!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.16" />-->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.1" />
</ItemGroup>
</Project>
Then I delete Startup.cs and change Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add DB Contexts and Services here
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
More detail information, see this link.
You can create a new empty project to migrate from .net 5 to .net 6 and see if the problem still exists.I think this may be the problem caused by some of your configuration, maybe you can check whether your path is correct?
Chen's answer is the correct migration step.
I tried to reproduce your issue. When I comment Index page method in HomeController. And I face the same issue.
So you probably deleted your "Index" action in HomeController.
The answer appears to be "screw you, start over."
I've tried copying my project code to a new .net 6 project -- that is, copying all the views, controllers, and whatnot to the new project and updating the new Program.cs file accordingly -- and I still got the same error. I then tried converting a different .net 5 project to .net 6, and got the same error again. So apparently, converting just doesn't work.
So be it. I quit. I've got too much else to do to worry about this. Thanks to those who responded to this post for trying to help resolve it, anyway.

using Serilog Asp.net core dotnet 5 preview 7

Serilog community.
Firstly, thank you for the great library!
I am trying out Asp.Net on Net 5 Preview 7 at the time of writing this question. I have created 2 web API projects one targeting [netcoreapp3.1] and another targeting [net5].
Below is my bootstrapping code, it is identical for both APIs
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Formatting.Elasticsearch;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(new ExceptionAsObjectJsonFormatter(renderMessage: true))
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
The problem is when I am looking at logs, I no longer see TraceId and SpanId for the API targeting [net5] with the DotNet 5 preview 7 SDK.
I also tested using the vanilla Logger and there was no issue there. Do I have to configure something, did I just miss something or is the DotNet 5 Preview SDK not fully supported yet?
Thanks for any info, much appreciated.
PS: Serilog Nuget packages used
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.2.0" />
Seems there was a change for logging in DotNet 5. Have a look at the GitHub issue linked here for the details,
serilog-aspnetcore github issue

Show NLog output in debug window of asp.net core app

Is it possible to show what NLog (or builit-in debugger) is logging in the Visual Studio 2017 debug window?
I have NLog set to output to a file but for development it would be really handy to be able to see the debug messages in the debug window. I can see articles written on how to do this with console but for asp.net project there isn't any console output, just the debug window.
The easy solution is just to use the OutputDebugString-target (Supported on NetCore)
https://github.com/NLog/NLog/wiki/OutputDebugString-target
Example:
<targets>
<target name="debugger" xsi:type="OutputDebugString" layout="${logger}::${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debugger" />
</rules>
Alternative one could use xsi:type="debugger":
https://github.com/NLog/NLog/wiki/Debugger-target
Yes, for Asp.Net Core, there are built-in logger providers like Console and Debug to write log to Output Window.
If you use WebHost.CreateDefaultBuilder(args), it will uses built-in providers Console and Debug, and you could check the output by Output window-> Asp.NET Core Web Server for a clean result.
For Getting started with ASP.NET Core 2 from NLog, it uses code below to clear all other log providers.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog() // NLog: setup NLog for Dependency injection
.Build();
If you also need log in the Debug window, you could modify code like below:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logger => {
logger.AddNLog();
//logger.AddConsole(); //UnComment out this line if you did not use CreateDefaultBuilder
})
.Build();

Microsoft.DotNet.Props directory does not exist for building xproj

We are trying to build an xproj project and an error about not being able to find the Microsoft.DotNet.Props file because it seems like its looking at the wrong directory.
Taking a look at the xml MSBuildExtensionsPath32 references C:\Program Files\dotnet\sdk\1.1.4\ where the directory Microsoft\VisualStudio\.. does not exist ... but the normal MSBuild directory C:\Program Files (x86)\MSBuild does have the directory for Microsoft.DotNet.Props file C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props
Here is the part of the XML
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
The error I see while building is:
error MSB4019: The imported project "C:\Program Files\dotnet\sdk\1.1.4\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
If anyone has any idea what is happening, help would be great
EDIT:
The build is invoked from a Jenkins project on Windows Server 2012 R2.
The VM image is from Azure market place "MicrosoftVisualStudio / VisualStudio / VS-2015-Comm-VSU3-AzureSDK-29-WS2012R2 / 2017.10.12" - which comes with Visual Studio 2015 community edition with update 3.
Azure SDK 2.9. Upgraded Node from old v0.12 to v8.x. Upgraded .NET core from not sure what was installed to 1.1.4.
The xproj itself has no code - except small amount in Startup.cs to serve static files (code at bottom of post).
The application is also used in a Service Fabric project. The error does not come from building the .sln but when packaging up the .sfproj (it might be its not set to build in the sln but packaging will need to build it).
Startup.cs:
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Website
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404
&& !Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/index.html";
await next();
}
});
app.UseStaticFiles();
}
}
}
Edit: here is the whole xproj xml
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<ProjectGuid>17107df8-0cfa-6946-917a-a9b8765cf9ea</ProjectGuid>
<RootNamespace>Website</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<IsServiceFabricServiceProject>True</IsServiceFabricServiceProject>
</PropertyGroup>
<ItemGroup>
<DnxInvisibleContent Include="bower.json" />
<DnxInvisibleContent Include=".bowerrc" />
</ItemGroup>
<ItemGroup>
<DnxInvisibleFolder Include="wwwroot\Angular\dist\" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b69-4b1e-b82e-3ada8210c987}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
You are trying to use a preview tooling (xproj) with the 1.1.4 version of the .NET Core Sdk. The preview tooling available in VS 2015 does not work with the 1.0+ stable tooling for .NET Core.
Make sure that a preview2 version of the .NET Core SDK is installed both on your development machines and Jenkins server - e.g. 1.0.0-preview2-003156 - and that a global.json file exists your solution directory to tell VS to use this preview version of the SDK:
{
"sdk": {
"version": "1.0.0-preview2-003156"
}
}
As a long-term solution I recommend moving to the stable and supported .NET Core tooling by migrating to VS 2017.

Customizing web.config for ASP.NET 5 (vNext) deployments

I'm using kpm pack to generate my deployment, which I deploy to Azure via ftp. I need to be able to serve static json files, so I need to add the following to my web.config:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="text/html" />
</staticContent>
</system.webServer>
The problem is that kpm pack generates the web.config, so the only way to accomplish this is to add the config section to the web.config after it's been generated. Since I'm doing automated deployments via ci, this would require a bit of effort. Is there a better way to accomplish this?
You should add your configurations to the source of web.config, instead of the target.
If you don't have a web.config in root of the project being packed, please create one. Then add your static content configurations to [project_root]/web.config.
"kpm pack" will preserve all configurations in [project_root]/web.config, add some information needed by IIS, and then write it to wwwroot/web.config.
Important Update:
A change was introduced in "kpm pack": https://github.com/aspnet/KRuntime/pull/972
Please move your web.config from project root to the source of wwwroot.
The source of wwwroot folder can be specified with 'webroot' in project.json (https://github.com/aspnet/Home/wiki/Project.json-file#webroot).
You can also specify it with '--wwwroot' option of "kpm pack".
In ASP.NET Core you may be able to avoid web.config altogether by configuring the static file middleware options (StaticFileOptions) in code, providing a custom FileExtensionContentTypeProvider as its ContentTypeProvider:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddInstance<IContentTypeProvider>(
new FileExtensionConentTypeProvider(
new Dictionary<string, string>(
// Start with the base mappings
new FileExtensionContentTypeProvider().Mappings,
// Extend the base dictionary with your custom mappings
StringComparer.OrdinalIgnoreCase) {
{ ".json", "text/html" }
}
)
);
...
}
public void Configure(
IApplicationBuilder app,
IContentTypeProvider contentTypeProvider)
{
...
app.UseStaticFiles(new StaticFileOptions() {
ContentTypeProvider = contentTypeProvider
...
});
...
}