How to return the ID of the log when logging with Microsoft.Extensions.Logging - serilog

In my .NET 6 web application I'm using the following infrastructure to log various events and exceptions:
Microsoft.Extensions.Logging as logging API
Serilog as logging framework
MS SQL database as loging sink (i.e. the target where the logs are going to be written).
In the API layer of the web application there's a middleware that catches all exceptions, logs them and returns the exception message as JSON string to the client.
Together with the exception message I'd like to return also the ID of the created log-entry to the client. Unfortunately, the methods of Microsoft.Extensions.Logging's LoggerExtension class (e.g. LogError()) don't return any value. Is there any way to obtain the ID of the created database record?

Related

How to configure Serilog for WCF

I am upgrading our old application for Serilog... One of the existing functionality is ... When log level = ERROR, it will log into local file and send 'WCF' request to the remote server, remote server will update database...
Basically it will log into multiple source(local file, remote database by sending wcf request) if it level is 'ERROR'.
I understand using 'rollingfile' appender to logging into local file.
However, i do not know how to configure 'WCF Service' for Serilog... is there any 'WCF SINK' can help me achieve this?
As of this writing there's no generic sink that makes "WCF" calls... You'd have to build your own sink, implementing the calls you need.
You can see a list of documented sinks in the "Provided Sinks" page on Serilog's wiki, and you can also see available sinks in NuGet.org.

Consolibyte QuickBooks PHP Library - How Do You Debug / View Errors?

I am currently trying to import a list of customers from my QB installation using the _quickbooks_customer_import_request() and _quickbooks_customer_import_response() methods found in Consolibyte's QB PHP library in the docs/web_connector/example_web_connector_import.php file.
When I run Web Connector, it is able to establish a connection and receive the request from my server. It then errors out on the response (where QB contacts my server and tries to pass to it response data). The error shown in Web Connector is a generic getLastError() message:
When I look in the quickbooks_log table that the Consolibyte library created in the quickbooks MySQL database, I see the following:
The above doesn't show the reason for the error. How do I log the underlying errors here? I would prefer a solution where the detailed error description can be inserted into the quickbooks_log table in a JSON format.
There's a Troubleshooting section of the docs here:
http://wiki.consolibyte.com/wiki/doku.php/quickbooks_integration_php_consolibyte?s[]=troubleshooting#troubleshooting
You should start by putting the Web Connector in VERBOSE mode, and looking at what's in the Web Connector log file.
Also, check your PHP error log.
There's many different places an error could be occurring (PHP, configuration, SSL/TLS, QuickBooks, etc.) so start with the Web Connector log and go from there.

Log failover with Serilog

Is it possible, using Serilog, to log to a webservice of mine and if throws an error (no internet, for instance) to log to a RollingFile.
Should only log to RollingFile if WebService fails.
You can implement this yourself by creating a custom sink that wraps another new RollingFileSink(...) and only forwards events if the web service call fails.
To do this you'd implement ILogEventSink or, if the web service accepts batches, create a subclass of PeriodicBatchingSink.

Unstable log4net emails with a Windows Service on a Windows Server 2003 R3 using Exchange Server

I am running a Windows Service on a Windows Server 2003 R2. We are using Exchange Server to send out the emails.
I am using log4net.dll 1.2.11.0.
I have situation where log4net sometimes sends emails and sometimes don't eventhough no changes has been made to the set up.
All the other log4net logging works very fine. And as said sometimes the application sends out emails and some times it don't, having made no change to the application.
All my methods are in try-catch clauses, but I don't get any errors.
When I run the the Windows Service on my local machine, the log4net email always works, and as said on the remote server, sometimes the log4net email works, sometimes it don't, having made no changes to the setup.
I am using log4net.Internal.Debug and have a System.Diagnostics.TextWriterTraceListener file where stuff is written to.
Scanning thorugh this file I haven't noticed anything in particular, but I don't know what specific to look for.
Any ideas about what the problem is or what to do?
If the SMTP appender cannot send an email it would log an exception with this text: Error occurred while sending e-mail notification. This would be visible if internal debugging is enabled. Maybe you have filters or something like that configured that prevents log4net from sending an email.
You could download the source code of log4net and add extra logging to the SMTP appender to find out if the "SendMail" method of the appender gets called at all. If it does and no email and no error is shown, then we need to assume that the exchange server somehow swallows the emails. If the appender is not triggered, then you need to review your filter / buffer / threshold configuration.
Alternatively you could try to use the SmtpPickupDirAppender.

Log errors in EventLog using ELMAH

I am using ELMAH to log errors in my asp.net MVC(C#) application.
I am able to log errors in xml or database. How to log errors to the eventlog using ELMAH?
ELMAH does not include an event log option.
ELMAH error log classes are not write-only; they also read the log data so that it can be displayed in the ELMAH web interface. Additionally, ELMAH logs more than just exception information. It also logs server variables, the form collection, and the information necessary to reproduce the yellow screen of death. Even if you were to log all of this information to the event log it would be difficult to read as plain text, and very difficult to read back in such a way that the ELMAH web interface could use it. If you are not going to use the ELMAH web interface then clearly that is not an issue.
If you want to log basic exception data to the event log you can create your own error log by subclassing ErrorLog. ELMAH supports multiple error logs, so you could continue to log detailed data to XML or a database (to service the ELMAH web interface) and then log a subset of that data to the event log.
Out of the box, you can't. You'd have to write a custom handler.
You can do something like this.
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("My Exception");

Resources