I want to log my whole data in a file and log only the errors in my oracle database. I'm using the below code but it's not working.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose().WriteTo.File("logs/myLog.txt", rollingInterval: RollingInterval.Minute)
.MinimumLevel.Error().WriteTo.Oracle(cfg =>
cfg.WithSettings(logConnectionString, tableSpaceAndTableName: "MY_TABLE")
.UseBurstBatch(true, 1000, true, 1)
.CreateSink())
.CreateLogger();
In this case, i log only error data both in file and oracle database. How must i configure the above code, to store the whole data in a file and the error data in database?
CodePudding user response:
Almost all sinks have a restrictedToMinimumLevel
parameter. There's no Serilog.Sinks.Oracle
project in the Serilog Github repo so I assume you use Serilog.Sinks.Oracle. The Oracle() method accepts a restrictedToMinimumLevel
parameter as well.
You can change your code to :
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/myLog.txt",
rollingInterval: RollingInterval.Minute,
restrictedToMinimumLevel:LogEventLevel.Verbose)
.WriteTo.Oracle(cfg =>
cfg.WithSettings(logConnectionString, tableSpaceAndTableName: "MY_TABLE")
.UseBurstBatch(true, 1000, true, 1)
.CreateSink(),
restrictedToMinimumLevel:LogEventLevel.Error )
.CreateLogger();
Configuring database logging typically takes more code than this, so it's a good idea to extract the code to a different method. You'll probably want to use additional columns to extract common attributes like categories, activity IDs etc so you don't have to parse the JSON payload to find specific events:
ILogEventSink ConfigureOracle(BatchLoggerConfiguration cfg)
{
const string column = "ADDITIONALDATACOLUMN";
var columnOptions = new ColumnOptions
{
AdditionalDataColumns = new List<DataColumn>
{
new DataColumn(column , typeof(string))
}
};
return cfg.WithSettings(logConnectionString, tableSpaceAndTableName: "MY_TABLE")
.UseBurstBatch(true, 1000, true, 1)
.CreateSink();
}
...
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/myLog.txt",
rollingInterval: RollingInterval.Minute,
restrictedToMinimumLevel:LogEventLevel.Verbose)
.WriteTo.Oracle(ConfigureOracle,LogEventLevel.Error )
.CreateLogger();
Organizing database logging code
I also log to a database. Putting everything into a single expression may be fashionable but the resulting code can quickly become too hard to read and maintain. Fluent APIs aren't always a good idea.
In this case, one almost always needs to specify extra columns to hold common properties like categories and activity IDs, perhaps even customer IDs. This means specifying extra columns. No matter the database product, querying the raw JSON data is more expensive than querying materialized and indexed columns.
It will take some experimentation until one get a table you can actually use for troubleshooting. The database logging configuration should be extracted into a separate a separate method, if not a separate file. Otherwise Startup.cs
or, in .NET 6, Program.cs
, will become an unreadable mess.