I'm trying to follow a tutorial on adding an Azure Cognitive Search service to my .NET Core app, and don't want to mess anything up. The tutorial says to add the following to my appsettings.json file:
{
"SearchServiceName": "<placeholder-Azure-Search-service-name>",
"SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
"AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string",
}
Problem is, my appsettings.json file already has stuff in it:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
When I try to add the Azure API call at the bottom it says only one top-level item is allowed, and if I assign some key to it inside the top-level json object I'm afraid the API won't work -- I don't use JSON very much and I'm new to .NET so sorry if this is a stupid question, I couldn't find any docs explaining what to do
It means your JSON is incorrect , you need to have it in the form of object with a key. Do something like
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Configuration": {
"SearchServiceName": "<placeholder-Azure-Search-service-name>",
"SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
"AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string"
}
}
Related
I'm just starting with Serilog.
Despite all the code samples/tut's, I've found online I just can't get it to output to file (the file isn't even created). My app is a Web API (.NET Core 3.1), I'm referencing
Serilog.AspNetCore(3.4.0)
Serilog.Settings.Configurations (3.1.0)
Serilog.Sinks.File (4.1.0)
My appsettings.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\DEV\\Logs\\mylog.txt",
"rollingInterval": "Day"
}
}
]
}
},
"AllowedHosts": "*"
}
My Program.cs
public static void Main(string[] args)
{
Serilog.Debugging.SelfLog.Enable(Console.Out);
//Read Configuration from appSettings
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
//Initialize Logger
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
try
{
CreateHostBuilder(args).Build().Run();
Log.Information("Application started!");
}
catch (Exception e)
{
Log.Fatal(e, "Application failed to start.");
}
finally
{
Log.CloseAndFlush();
}
}
My controller
public void Post(SampleRequest request)
{
Log.Information("Received request {#request}", request);
}
Not even the Selflog is writing anything to Visual Studio output console Serilog.Debugging.SelfLog.Enable(Console.Out);
Try and enable SelfLog which should help you pinpoint what is going wrong. This call is slightly different to yours.
Add this in Program.cs just after you call .CreateLogger();
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
More details - https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics
I have Serilog logging to a rolling file in a .Net Core 3.1 app.
These are my nuget references:
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
Also, just noticed you don't seem to have a Using section in your appsettings.json:
"Serilog": {
"Using": [ "Serilog.Sinks.RollingFile" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:\\Logs\\ScreenPop\\Log-{Date}.txt"
}
}
]
},
I figured out what was the problem. I copied settings from some blog and the "WriteTo" element was actually nested inside the "MinimumLevel" one.
The correct settings would be:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\DEV\\Logs\\mylog-.txt",
"rollingInterval": "Day"
}
}
]
},
"AllowedHosts": "*"
}
I have developed a web application in .Net Core 3.1 (Web API) using Entity Framework 3.1, Angular 10.0 and SQL Server as database.
I have followed code first approach and everything was fine until I deployed my app to IIS on a virtual machine. When I am trying to login to my app for the first time noticed the below error response in the chrome developer console while connecting with my database -
AppSettings.json -
{
"AppSettings" : {
"Token" : "******"
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=datingapp;User Id=appuser;Password=*****"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
}
UPDATE : Similar error from postman as well.
Please advise what is wrong with my connection string or could it be something else ?
Thank you
I'm using a webapi .netcore project.
I want to put all the cross settings in the appsettings.json file.
How do I do this?
This is my code:
app.UseCors(x => x.WithOrigins("http://localhost:4200")
.AllowCredentials()
.WithHeaders("content-type")
.WithMethods("GET", "POST", "PUT", "DELETE"));
If you want to set the CORS settings in appsettings.json and use the settings in startup.cs, you can follow the code below:
This is my appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"AllowedOrigins": "http://localhost:4200",
"AllowedHeaders": "content-type",
"AllowedMethods": "GET,POST,PUT,DELETE"
}
This is my partial code in startup.cs:
app.UseCors(x => x.WithOrigins(Configuration.GetSection("AllowedOrigins").Value.Split(","))
.AllowCredentials().WithHeaders(Configuration.GetSection("AllowedHeaders").Value.Split(","))
.WithMethods(Configuration.GetSection("AllowedMethods").Value.Split(",")));
app.UseHttpsRedirection();
the application uses Microsoft.Extensions.Logging
_logger.LogError
_logger.LogInformation
this is configured in appsettings.json
"Logging": {
"IncludeScopes": false,
"Console": {
"LogLevel": {
"Default": "Information"
}
}
},
then I am using mountebank as config server.
at imposter file
"propertySources": [
{
"source": {
"Logging.Console.LogLevel.Default": "Information",
}
How do I enable all log to include error as well?
I upgraded to .net core 2.2 and EF 2.2 as well. Now when i fetch a table from DbSet, for each row i have in my table there a log line and it's taking forever. How to disable this since i didnt have this logs in .net core 2.0 ?
ex : for the log line i have this for each row :
[15:58:02 DBG] Context 'BookContext' started tracking 'Book' entity.
Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see key values.
And since my table has 23000 records, i have 23000 log line as well !
My serilog log config looks like this :
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Debug"
}
},
you can make like this
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft.EntityFrameworkCore.Database.Command": "Error",
"Microsoft": "Debug",
}
},
I use below config in my appsettings.json. #Khaled's answer was having wrong level of json format. Thank you it helps.
I hope this will help someone who end up on this thread.
My config was like below:
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Debug",
"Override": {
"Microsoft.EntityFrameworkCore.Database.Command": "Error",
"Microsoft": "Debug"
},