How Do I Implement Custom Logging?
Q: How Do I Implement Custom Logging?
Note that the Catalog app has a complete code example demonstrating custom logging.
Here is how to implement logging to the Visual Studio output window.
Implement a class derived from ICustomLogger
, overriding the various log methods for each log level, Warn
, Error
, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class CustomLogger : ICustomLogger { private static void Log(string tag, string message) { System.Diagnostics.Debug.WriteLine($"{tag}: {message}"); } public void Critical(string tag, string message) { Log(tag, message); } public void Error(string tag, string message) { Log(tag, message); } public void Warn(string tag, string message) { Log(tag, message); } public void Info(string tag, string message) { Log(tag, message); } public void Debug(string tag, string message) { Log(tag, message); } public void Trace(string tag, string message) { Log(tag, message); } } |
Then assign an instance of it to the PSPDFKit Logger
.
1 2 3 4 5 6 7 8 9 10 11 12 | // Set up your own custom logger var customLogger = new CustomLogger(); // Set it in the PSPDFKit logger var logger = Logger.Instance; logger.CustomLogger = customLogger; // Choose your own log level. Debug is useful for sending reports to us. logger.CurrentLevel = Level.Debug; // You can use the logger for your own logs too. logger.Info("MYAPP", "Created customer logger and set log level to Debug"); |
Consider logging your application to a file that users can send to you. This typically saves our customers a lot of time when diagnosing issues.