To enable logging, use the ConsoleLogger class or implement a custom logger via the ILogger interface.
The ILogger interface declares the methods used to perform logging.
The ConsoleLogger class implements the methods that output log messages to a standard console.
The log contains the following types of messages:
Error - for unrecoverable exceptions
Warning - for recoverable/expected exceptions
Trace - for general information
Logging to the console
To log to a standard console, use the ConsoleLogger class implementation:
usingGroupDocs.Conversion;usingGroupDocs.Conversion.Logging;usingGroupDocs.Conversion.Options.Convert;stringdocumentPath=@"sample.docx";// NOTE: Put here actual path to your documentstringoutputPath=@"converted.pdf";// Create console loggerConsoleLoggerlogger=newConsoleLogger();// Create ConverterSettings and specify logger.Func<ConverterSettings>settingsFactory=()=>newConverterSettings{Logger=logger};using(Converterconverter=newConverter(documentPath,settingsFactory)){PdfConvertOptionsconvertOptions=newPdfConvertOptions();converter.Convert(outputPath,convertOptions);}
Console output would be as follows:
[TRACE] Determine loaded for source document sample.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document sample.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of sample.docx...
[TRACE] ... pipeline selected ...
[TRACE] ... conversion completed.
Implementing custom logger
To log to a file, a stream or any other custom location, implement the ILogger interface:
For trace messages, implement the public void Trace(string message) method.
For warning messages, implement the public void Warning(string message) method.
For error messages, implement the public void Error(string message) method.
The following code snippet shows how to implement a simple file logger:
usingGroupDocs.Conversion;usingGroupDocs.Conversion.Logging;usingGroupDocs.Conversion.Options.Convert;stringdocumentPath=@"sample.docx";// NOTE: Put here actual path to your documentstringoutputPath=@"converted.pdf";// Create logger and specify the output fileILoggerlogger=newCustomFileLogger("log.txt");// Create ConverterSettings and specify logger.Func<ConverterSettings>settingsFactory=()=>newConverterSettings{Logger=logger};using(Converterconverter=newConverter(documentPath,settingsFactory)){PdfConvertOptionsconvertOptions=newPdfConvertOptions();converter.Convert(outputPath,convertOptions);}publicclassCustomFileLogger:ILogger{privatereadonlystring_fileName;/// <summary>/// Create logger to file./// </summary>/// <param name="fileName">Full file name with path</param>publicCustomFileLogger(stringfileName){_fileName=fileName;}/// <summary>/// Writes trace message to file./// 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){File.AppendAllText(_fileName,$"[TRACE] {message}{Environment.NewLine}");}/// <summary>/// Writes warning message to file./// 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){File.AppendAllText(_fileName,$"[WARN] {message}{Environment.NewLine}");}/// <summary>/// Writes an error message to file./// 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){File.AppendAllText(_fileName,$"[ERROR] {message}, exception: {exception}{Environment.NewLine}");}}
The content of the log.txt file would be as follows:
[TRACE] Determine loaded for source document sample.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document sample.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of sample.docx...
[TRACE] ... pipeline selected ...
[TRACE] ... conversion completed.
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.