I'm trying to determine if it's possible to configure the column options for serilog sink mssqlserver in the appsettings.json file for an ASP.Net Core 2 project.
I create and configure the logger in the Program.cs file.
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
//.Enrich.WithProperty("AppName", "One Badass App") // Adds property to XML structure in properties column
.ReadFrom.Configuration(Configuration)
.CreateLogger();
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
I can build the configuration file from the appsettings.json file, which contains a Serilog node with information for which connection string and table to use.
{
"AppSettings": {
"Application": {
"Name": "Payment Processing API",
"Version": "1.0"
}
},
"ConnectionStrings": {
"localPaymentProcessingDb": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=(localdb)\\mssqllocaldb;Database=PaymentProcessing;Trusted_Connection=True;MultipleActiveResultSets=true",
"tableName": "Logs"
}
}
]
}
There's an open issue on Github for this, but I haven't found any other information about it.
If column options can't be configured in the appsettings.json, where and how should they be configured in an ASP.Net Core 2 project?
If anyone else stumbles across this same issue the linked GitHub issue in the question now contains the answer:
This is now possible with the latest SQL sink and
Serilog.Settings.Configuration packages.
Also, I know this question asks for a .NET Core 2 answer, and it sounds like the linked GitHub page answers this for 2, I am using .NET Core 3.1 and the following worked for me (hopefully it works for 2 as well)
I installed the Serilog.Settings.Configuration Nuget package and used the following Serilog appsettings.json configuration:
"Serilog":{
"MinimumLevel":"Information",
"WriteTo":[
{
"Name":"MSSqlServer",
"Args":{
"connectionString":"DbContext",
"tableName":"EventLog",
"autoCreateSqlTable":true,
"columnOptionsSection":{
"addStandardColumns":[
"LogEvent"
],
"removeStandardColumns":[
"MessageTemplate",
"Properties"
]
}
}
}
]
}
For accessing Serilog from appsettings in Code, you could bind Serilog node to Serilog class.
Here are the detail steps.
1. Create Serilog Configuration Class
public class SerilogConfiguration
{
public LogEventLevel MinimumLevel { get; set; }
public List<WriteTo> WriteTo { get; set; }
}
public class WriteTo
{
public string Name { get; set; }
public Args Args { get; set; }
}
public class Args
{
public string ConnectionString { get; set; }
public string TableName { get; set; }
public List<StandardColumn> Add { get; set; }
public List<StandardColumn> Remove { get; set; }
}
Configure appsettings.json
Code:
{
"Serilog": {
"MinimumLevel": "Error",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "<our connection string>",
"tableName": "Log",
"Remove": [ "Properties" ],
"Add": [ "LogEvent" ]
}
}
]
}
}
Bind appsetting.json to class
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SerilogConfiguration>(Configuration.GetSection("Serilog"));
}
Access Configuration
public class HomeController : Controller {
private readonly SerilogConfiguration _configuration;
public HomeController(IOptions<SerilogConfiguration> configuration)
{
_configuration = configuration.Value;
}
public IActionResult AppSettings()
{
var columnOptions = new ColumnOptions();
var MSSqlServer = _configuration.WriteTo.Where(wt => wt.Name == "MSSqlServer").FirstOrDefault();
// Don't include the Properties XML column.
foreach(var columnRemove in MSSqlServer.Args.Remove)
{
columnOptions.Store.Remove(columnRemove);
}
// Do include the log event data as JSON.
foreach (var columnAdd in MSSqlServer.Args.Add)
{
columnOptions.Store.Add(columnAdd);
}
Log.Logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(MSSqlServer.Args.ConnectionString, MSSqlServer.Args.TableName, columnOptions: columnOptions,
restrictedToMinimumLevel: _configuration.MinimumLevel)
.CreateLogger();
return Ok("OK");
} }
Related
I have an azure function, it logs the information without any issues.
namespace aspnetcore_azurefun_blob
{
[StorageAccount("AzureWebJobsStorage")]
public class FileTrigger
{
#region Property
private readonly IFileProcessor fileProcessor;
#endregion
#region Constructor
public FileTrigger(IFileProcessor fileProcessor)
{
this.fileProcessor = fileProcessor;
}
#endregion
[FunctionName("FileTrigger")]
public void ProcessFilesFromSamplesContainer([BlobTrigger("samples-workitems/{name}")]Stream myBlob, string name, ILogger log, ExecutionContext context)
{
log.LogInformation("Function: ProcessFilesFromSamplesContainer is called");
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var result = fileProcessor.ProcessAsync(myBlob, name);
log.LogInformation($"Function Completed Successfully on {DateTime.Now.ToLongDateString()} # {DateTime.Now.ToShortTimeString()}.\n.");
}
However, I also have the business logic implemented using DI and below is the excerpt of the implementation.
ServiceBase.cs
namespace BusinessService.Services.Common
{
public abstract class ServiceBase<T>
{
public static AppDbContext AppDbContext;
public static ILogger<T> Logger { get; set; }
public static AppConfigurator Configurator { get; set; }
public ServiceBase(AppDbContext appDbContext, ILogger<T> logger, IOptions<AppConfigurator> configurator)
{
AppDbContext = appDbContext ?? throw new ArgumentNullException(nameof(appDbContext));
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
Configurator = configurator.Value ?? throw new ArgumentNullException(nameof(configurator));
}
}
}
FileProcessingService.cs
namespace BusinessService.Services
{
public interface IFileProcessingService
{
void Process(Stream myBlob, string name);
}
public class FileProcessingService : ServiceBase<FileProcessingService>, IFileProcessingService
{
#region Constructor
public FileProcessingService(AppDbContext appDbContext, ILogger<FileProcessingService> logger, IOptions<AppConfigurator> configurator)
: base(appDbContext, logger, configurator) { }
#endregion
#region Public Methods
public void Process(Stream myBlob, string name)
{
AppDbContext.FileRecords.Add(new FileRecords
{
FileName = name,
IsCompleted = DefaultValues.IsCompleted
});
AppDbContext.SaveChanges();
Logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
Logger.LogInformation("Database is updated..!");
}
#endregion
}
}
Line#34 and #35 doesn't Log anything
Logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
Logger.LogInformation("Database is updated..!");
DependencyRegistrar.cs
namespace CrossCutting.DependencyInjection
{
public static class DependencyRegistrar
{
public static void Intialize(this IServiceCollection services)
{
// Initialize App Settings from Configurator Settings Json file
services.AddOptions<AppConfigurator>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection("AppConfigurator").Bind(settings);
})
.Validate((c) =>
{
return !new[] { c.AzureSQLServerConfigurator.ConnnectionString }.Any(s => String.IsNullOrWhiteSpace(s));
});
}
}
}
What am I missing so that FileProcessingService.cs will log the information ?
I have checked your code in our end I could get the logging information. In your code i have noticed in your code you are using the Logger instead of logger.
Because in your Region Constructor you are using ILogger<FileProcessingService> logger from here you have to call the logger to push your logging information into Application Insights/Function execution panel (Output console window)
#region Constructor
public FileProcessingService(AppDbContext appDbContext, ILogger<FileProcessingService> logger, IOptions<AppConfigurator> configurator)
: base(appDbContext, logger, configurator) { }
#endregion
#region Public Methods
public void Process(Stream myBlob, string name)
{
AppDbContext.FileRecords.Add(new FileRecords
{
FileName = name,
IsCompleted = DefaultValues.IsCompleted
});
AppDbContext.SaveChanges();
# changed Logger into logger
logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
logger.LogInformation("Database is updated..!");
}
#endregion
Still if not able to push the logs you can add your namespace in your host.json file to avoid missing the logging
{
"version": "2.0",
"logging": {
"logLevel": {
// Here you can use your Project namespace like BusinessService.Services
"<namespace>": "Information"
}
}
}
I'm using the following nuget package : https://github.com/msmolka/ZNetCS.AspNetCore.IPFiltering
I'm using it to block IP that are trying to bruteforce the authentication of my app.
The Blacklist is defined in the appsetting.json, and I don't know how to dynamicaly modify it during runtime, for example, add an IP that has a bad password.
The way I'm actually doing isn't working as I still can connect even if the IP is correctly persisted in the conf ...
Startup.cs
namespace Sondage
{
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.AddIPFiltering(this.Configuration.GetSection("IPFiltering"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseIPFiltering();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"IPFiltering": {
"DefaultBlockLevel": "None",
"HttpStatusCode": 404,
"Blacklist": [],
"IgnoredPaths": [ "GET:/ignoreget", "*:/ignore" ]
}
}
Part of my controller :
[Route("api/authenticate")]
[ApiController]
public class authenticationController : ControllerBase
{
private IConfiguration _configuration;
public authenticationController(IConfiguration Configuration)
{
_configuration = Configuration;
}
[HttpPost]
public string authent(string value)
{
Dictionary<string, string> result = new Dictionary<string, string>();
IPAddress ip_addr = HttpContext.Connection.RemoteIpAddress;
if(!Globals.tryByIP.TryGetValue(ip_addr, out int numberOfTry)) {
Globals.tryByIP.Add(ip_addr, 0);
} else
{
if(numberOfTry>=0)
{
Console.WriteLine("-----");
_configuration.GetSection("IPFiltering")["Blacklist"] = ip_addr.ToString();
Console.WriteLine(_configuration.GetSection("IPFiltering")["Blacklist"]);
}
}
Make sure to use the IPAddressRange package as well.
I had problems recently with this nuget package as well, and at first I forgot to add the ipaddress parser.
I am a new ASP Core developer.
I want to make a sign-in method and update my database but I get
No database provider has been configured for this DbContext asp core
I tried a lot of things, but nothing works.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<StudentSelectionContext>();
services.AddMvc();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(option => {
option.LoginPath = "/User/LogIn";
option.AccessDeniedPath = "/Error/AccessDenied";
option.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
}
and that my StudentSelectionContext
public StudentSelectionContext()
{
}
public StudentSelectionContext(DbContextOptions<StudentSelectionContext> options)
: base(options)
{
}
public virtual DbSet<Department> Department { get; set; }
public virtual DbSet<FinalDistribution> FinalDistribution { get; set; }
public virtual DbSet<InfoTable> InfoTable { get; set; }
public virtual DbSet<Roles> Roles { get; set; }
public virtual DbSet<StudnetGpa> StudnetGpa { get; set; }
public virtual DbSet<StuSelection> StuSelection { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
}
}
so what is the problem?
You need to configure DataBase Provider while using AddDbContext.
You could refer this Database Providers to find the available database provider.
For development mode, you may try SQL Server Express, and you could try code below:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
The ConnectionString is configured in appsettings.json
I am using Visual Studio 2017, Version 15.7.2 and working with an answer in the following link:
net core 1 (dnx 4.5.1) with enterpriselibrary 6 - setting up the connection string
public class DataConfiguration
{
public string DefaultDatabase { get; set; }
public List<ConnectionStringSettings> ConnectionStrings { get; set; }
}
public class Startup
{
public Startup(IConfiguration configuration)
{
//Get the Database Connections from appsettings.json
DataConfig = configuration.Get<DataConfiguration>();
var defaultDb = DataConfig.ConnectionStrings?.Find(c => c.Name == DataConfig.DefaultDatabase);
DatabaseFactory.SetDatabases(() => new SqlDatabase(defaultDb.ConnectionString), GetDatabase);
Configuration = configuration;
}
public Database GetDatabase(string name)
{
var dbInfo = DataConfig.ConnectionStrings.Find(c => c.Name == name);
if (dbInfo.ProviderName == "System.Data.SqlClient")
{
return new SqlDatabase(dbInfo.ConnectionString);
}
return new MySqlDatabase(dbInfo.ConnectionString);
}
The part I cannot get to work is:
//Get the Database Connections from appsettings.json
DataConfig = configuration.Get<DataConfiguration>();
configuration.Get<DataConfiguration>() returns an empty instance of DataConfiguration. I expected it to populate or bind to DataConfiguration in the appsettings file.
My appsettings.json:
{
"DataConfiguration": {
"DefaultDatabase": "MyDB",
"ConnectionString": "Server=MyServer;Database=MyDB;Integrated Security=True;MultipleActiveResultSets=true",
"ProviderName": "System.Data.SqlClient"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
My Startup.cs
public class DataConfiguration
{
public string DefaultDatabase { get; set; }
public string ConnectionString { get; set; }
public string ProviderName { get; set; }
}
public class Startup
{
public DataConfiguration DataConfig;
public Startup(IConfiguration configuration)
{
DataConfig = configuration.Get<DataConfiguration>();
DatabaseFactory.SetDatabases(() => new SqlDatabase(DataConfig.ConnectionString), GetDatabase);
Configuration = configuration;
}
public Database GetDatabase(string name)
{
if(DataConfig.ProviderName == "System.Data.SqlClient")
{
return new SqlDatabase(DataConfig.ConnectionString);
}
return null;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var config = new DataConfiguration();
Configuration.Bind("DataConfiguration", config);
services.AddMvc();
services.AddSingleton(config);
//services.AddSingleton<IConfiguration>(Configuration);
services.AddTransient<IPatientRepository, PatientRepository>();
}
What am I missing?
So, if you want use settings file this way, you have to write:
var DataConfig = new DataConfiguration();
Configuration.GetSection(nameof(DataConfiguration)).Bind(DataConfig);
But usually settings and configurations uses another way:
https://learn.microsoft.com/aspnet/core/fundamentals/startup?view=aspnetcore-2.0
or even easier:
services.Configure<DataConfiguration>configuration.GetSection(nameof(DataConfiguration)))
services.AddScoped(serviceProvider =>
{
var options = serviceProvider.GetRequiredService<IOptions<DataConfiguration>>();
return new SqlDatabase(options.Value.ConnectionString);
});
I hope its clear, but maybe final example should be slightly corrected :)
I am getting very confused with the Funq container.
I have the following:
public interface IConnectionString
{
string ConnectionString { get; set; }
}
public class FoundationConnection : IConnectionString
{
public FoundationConnection(string connectionString)
{
ConnectionString = connectionString;
}
public string ConnectionString { get; set; }
}
Now in my AppHost, I would like to register
container.Register<IConnectionString>(c=> new FoundationConnection(AppSettings.Get(
"FoundationConnectionString", "").MapHostAbsolutePath()));
In my ServiceInterface I want to call this injected method somehow:
public class ServiceInterface : Service
{
public IConnectionString foundationConnection { get; set; }
public object Any(SomeRequest request)
{
string injectedProperty = foundationConnection.ConnectionString;
}
}
}
Issue is that foundationConnection is null and never injected.
I hope this makes sense?
Personally I would use AppSettings to access config settings which would allow you to source configuration from a number of different and cascading configuration sources.
But I've tested this using these types:
public interface IConnectionString
{
string ConnectionString { get; }
}
class FoundationConnectionString : IConnectionString
{
public FoundationConnectionString(string connectionString)
{
ConnectionString = connectionString;
}
public string ConnectionString { get; set; }
}
and it's working correctly after registering it in the AppHost.Configure():
public override void Configure(Container container)
{
container.Register<IConnectionString>(c =>
new FoundationConnectionString("My Connection"));
}
and accessing it from a test service:
[Route("/test")]
public class Test : IReturn<string> { }
public class TestService : Service
{
public IConnectionString Config { get; set; }
public object Any(Test request)
{
return Config.ConnectionString;
}
}
Which returns "My Connection" when called.