Xamarin Forms: Logging with anything from Console to SQLite
And it’s also logging the invoked method name, and the file name containing the method!
My Forms.RecurrenceToolkit NuGet package pack is now extended with logging functionality.
You can use the pre-written Console and SQLite logger without writing much code, or you can implement your own logger in a few lines, and use it instantly simultaneous with other loggers.
Install banditoth.Forms.RecurrenceToolkit.Logging.* packages, and enjoy the painless logging, and focus on the great ideas instead of being a copy paste robot. 🙂
Usage
In your App.xaml.cs
, initalize the logger like:
LoggingProvider.Initalize(
// If you have installed the console logger:
new ConsoleLogger(),
// If you have installed SQLite Logger:
new SQLiteLogger()
);
The logger is including the calling method’s name, and the .cs file name in the logs. You can access the logger from anywhere by calling these methods:
LoggingProvider.LogCritical("It's a critical message");
LoggingProvider.LogDebug("It's a debug message");
LoggingProvider.LogError("It's an error message");
LoggingProvider.LogException(new Exception(), "It's an exception");
LoggingProvider.LogInformation("It's an information message");
LoggingProvider.LogTrace("It's a trace message");
LoggingProvider.LogTrace(new StackTrace());
LoggingProvider.LogWarning("It's a warning message");
By default, the console and the SQLite logger logs exceptions in error level.
You can implement your own logger by deriving from BaseLogger
class, like:
public class CustomLogger : BaseLogger
{
public CustomLogger() : base(new LoggerOptions()
{
IncludeCallerSourceFullFileName = true, // This will print C:/Users/Path/AssemblyFile.cs
IncludeCallerSourceShortFileName = false, // This will print AssemblyFile.cs
ExceptionLevel = Enumerations.LogLevel.Error, // The LogExceptions calls routed to log to the loglevel set.
IncludeCallerMethodName = true // This can print the calling method's name
})
{
}
public override void LogCritical(string criticalMessage, string callerMethod, string filePath)
{
// Your own method
}
// .. File continues
Follow for more
Tags In
1 Comment
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Hi, I am András,
I am a seasoned software engineer from Budapest, Hungary with a strong focus on mobile app development using .NET MAUI and Xamarin.Forms. My expertise also extends to website building for my happy customers and other complex system designing. I am passionate about developing well-organized, maintainable software solutions.
Great! You saved my day.