By default logging is disabled when processing documents but product provides a way to specify custom logging implementationsave and set up this instance.
There is an interface that we can utilize:
ILogger - defines the interface for logging different process event like errors, warnings and information messages (traces).
There are 3 types of messages in the log file:
Error - for unrecoverable exceptions
Warning - for recoverable/expected/known exceptions
Trace - for general information
Implementing custom logger
To make your logger you should implement ILogger interface.
For trace messages - implement public void Trace(string message) method For warning messages - implement public void Warning(string message) method For error messages - implement public void Error(string message) method
In this example, we’ll implement a simple file logger.
// Create logger and specify the output filevarlogger=newCustomLogger("output.log");// Create SignatureSettings and specify FileLoggervarsettings=newSignatureSettings(fileLogger);using(varsignature=newSignature("sample.docx",settings)){varoptions=newQrCodeSignOptions("JohnSmith");// sign document to filesignature.Sign(outputFilePath,options);}/// <summary>/// Writes log messages to API endpoint./// </summary>publicclassAPILogger:ILogger{privateobject_lock=newobject();privateHttpClient_client;/// <summary>/// Create logger to API endpoint./// </summary>publicAPILogger(){_client=newHttpClient(){BaseAddress=newUri("http://localhost:64195/")};_client.DefaultRequestHeaders.Accept.Clear();_client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));}/// <summary>/// Writes an error message to the console./// Error log messages provide information about unrecoverable events in application flow./// </summary>/// <param name="message">The error message.</param>/// <param name="exception">The exception.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="exception"/> is null.</exception>publicvoidError(stringmessage,Exceptionexception){if(message==null)thrownewArgumentNullException(nameof(message));if(exception==null)thrownewArgumentNullException(nameof(exception));varline=$"{message}. Exception: {exception}";PostMessage(LogLevel.Error,line);}/// <summary>/// Writes trace message to the console./// Trace log messages provide generally useful information about application flow./// </summary>/// <param name="message">The trace message.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>publicvoidTrace(stringmessage){if(message==null)thrownewArgumentNullException(nameof(message));PostMessage(LogLevel.Trace,message);}/// <summary>/// Writes warning message to the console;/// Warning log messages provide information about the unexpected and recoverable event in application flow./// </summary>/// <param name="message">The warning message.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>publicvoidWarning(stringmessage){if(message==null)thrownewArgumentNullException(nameof(message));PostMessage(LogLevel.Warning,message);}privatestringPostMessage(LogLevellevel,stringmessage){stringresult=string.Empty;stringhdrs="INFO";switch(level){caseLogLevel.Warning:hdrs="WARNING";break;caseLogLevel.Error:hdrs="ERROR";break;}stringdate=DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");stringline=$"GroupDocs.Signature {hdrs} [{date}]. Message: {message}";varquer=newStringContent(line);lock(this._lock){HttpResponseMessageresponse=_client.PostAsync("api/logging",quer).Result;response.Content.Headers.ContentType=newMediaTypeHeaderValue("application/json");response.EnsureSuccessStatusCode();result=response.Content.ReadAsStringAsync().Result;}returnresult;}}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.