Closed IzaacJ closed 2 months ago
@IzaacJ did you check our sample applications in the repo under samples/? Especially WorkerServiceDemo does exactly what you are trying to do.
Did you also try the recommendations from the troubleshooting section in our README?
During the sink initialization you would only get an exception if you have autocreation of the db and/or table enabled and there is some problem during that operation.
I did check yes, but I have no Idea on WHAT options that are actually required. From my understanding of the documentation I figured I'd just copy-paste our currently working MSSQLServer sink configuration, minus the restrictedToMinimumLevel, since the docs said it is not supported by the audit sink.
I've read through the troubleshooting FAQ, yes, but I have not checked the selflog, and I'm always running outside of vsstudio (M2 Mac + Rider + Terminal).
Shouldn't the sink initialisation bugger out when there isn't even a running server to connect to, no matter if autocreating is enabled?
Also, that last statement, I hope that I'm misinterpreting that, but do you mean that when all is well and up and running, connected to an existing server and database and table and all that, and the database server goes down, min-running the application, it would not throw an exception when performing audit logging, because the server was available when the application was booting up?
Never had any issues before, but yeah, the selflog helped. Doh. Was some culture invariant thingy that was enabled in the project file that caused the entire sql client to bug out. That's what you get when you hand-copy the base code structure from an air-gapped system for testing implementations... At least my first concern is resolved, I think xD
- How would I make ordinary logging and audit logging in-code, with this logger, assuming I actually configured it correctly?
In the WorkerServiceDemo app audit and batched logging are both configured (but in appsettings.json config). If you want to do the same thing programmatically, you create an instance of LoggerConfiguration. To configure the logging pipeline you can use WriteTo() to add batched loggers and AuditTo() to add audit loggers.
Log.Logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(...)
.AuditTo.MSSqlServer(...)
.CreateLogger();
Please look in our samples code and tests for more complete examples including the parameters you have to pass to the sink init methods.
- And most critically, when audit logging with the above configuration, it should fail and throw an exception, yes?
Sorry, I think i misunderstood your question 3 a bit before.
Yes, you are right. If you use an audit sink, the log entry is synchronously written to the DB when you call a log method. If there is a problem while doing that (e.g. the DB is unreachable) you will get an exception.
I've looked at that specific demo a lot, but I still cannot figure out how to explicitly make an audit log or explicitly make an ordinary log.
try {
Log.Information("How to make this an ordinary log!?");
Log.???("How to make this an audit log!? That should fail due to non-existing database!");
} catch {
Log.Error("And this should be in the ordinary log...");
return 1;
}
Now I see what your questions is aiming at. There is no separate log method or parameter to explicitly write to an audit logger. Any log event you create is going through the whole logging pipeline you have configured. This means if you have configured your pipeline with WriteTo()
and AuditTo()
at the same time, any log will be sent to the batched- AND the audit loggers you configured.
If you need to access them separately, you could create two distinct logger objects with two different pipelines: one only with WriteTo() using only the batched sink and the other only with AuditTo() using only the audit sink. In your code you can then use them selectively to log.
This is actually a topic of general Serilog configuration and not MSSQL sink specific. With some luck you might find a sample doing exactly what you need in some of the documents of Serilog core or in other sources like Stackoverflow.
Okey! Thanks for the help! :D
I'm having issues with getting audit logging to work, and I highly assume that I am the one missing something here, probably in the appsettings.json configuration or in how to actually make the audit log entries in-code. And I'm explicitly setting it up with a non-existing database, because we really need to know when the audit logging fails.
In appsettings we have this:
And I'm creating the logger like this:
And my main concerns are: