Uncle also said Xamarin ~ Android ~ Log Record, xamarinandroid
No matter which platform or application you start, logs are always indispensable. you can also see uncle's log component in DDD. During mobile development in xamarin, logs are also required to better debug and record running conditions, this section mainly describes the implementation of log components during xamarin for android development.
The android platform has its own log component, which is mainly output in the output window. Sometimes we need it to write logs to the disk. for mobile phones, it can be sdCard, in this case, we need to encapsulate a log component.
Still Object-Oriented Design
The above is the log component created in the Android binding class library. Our Jar native component can also be placed in this binding class library project. You can directly reference this project during development.
Simplified log behavior and level
/// <Summary> // log function interface specification // </summary> public interface ILogger {# region-level log // <summary> // record the message log File // </summary> /// <param name = "message"> </param> void Logger_Info (string message ); /// <summary> /// log of abnormal occurrence /// </summary> /// <param name = "message"> </param> void Logger_Error (Exception ex ); /// <summary> /// logs generated during debugging /// </summary> /// <param name = "message"> </param> void Logger_Debug (string message ); /// <summary> /// logs that cause program termination /// </summary> /// <param name = "message"> </param> void Logger_Fatal (string message ); /// <summary> /// the log that generates the warning /// </summary> /// <param name = "message"> </param> void Logger_Warn (string message ); # endregion}
/// <Summary> /// Log Level: DEBUG | INFO | WARN | ERROR | FATAL | OFF /// </summary> internal enum Level {// <summary> // record DEBUG | INFO | WARN | ERROR | FATAL-level logs /// </summary> DEBUG, /// <summary> // record INFO | WARN | ERROR | FATAL-level log // </summary> INFO, /// <summary> /// record WARN | ERROR | FATAL-level logs /// </summary> WARN, /// <summary> /// ERROR | FATAL-level log // </summary> ERROR, /// <summary> /// records FATAL-level logs /// </summary> FATAL, /// <summary> /// disable the log function /// </summary> OFF ,}
LoggerBase is the base class for log implementation. It has been introduced in Lind. DDD. Here we only talk about the file path selection. android and pc are somewhat different, because the former has the concept of SD card.
/// <Summary> /// log file address /// stored on the mobile phone without sdCard /// </summary> protected string FileUrl {get {if (Android. OS. environment. getExternalStorageState (Android. OS. environment. rootDirectory ). equals (Android. OS. environment. mediaMounted) return "/" + Android. OS. environment. mediaMounted; else return "/Logger ";}}
Singleton mode, allowing your program to create a log instance object only once
There is only one log implementation method that I can write to death. There will be a variety of log implementation methods in the future, which can be written in the configuration.
/// <Summary> /// Log Production class /// Singleton mode and Policy mode and factory mode /// </summary> public sealed class LoggerFactory: when ILogger {# region Logger has multiple implementations, you must use the Singleton mode. // <summary> // you cannot create a class instance externally. // </summary> private LoggerFactory () {string type = "file"; switch (type) {case "file": iLogger = new NormalLogger (); break; default: throw new ArgumentException ("Please correctly configure the LoggerType node of mongoetting (file, log4net, mongodb )");}} /// <summary> /// Log Level /// </summary> private static level = Level. DEBUG; /// <summary> /// thread lock /// </summary> private static object lockObj = new object (); /// <summary> /// log factory /// </summary> private static LoggerFactory instance; /// <summary> // log provider, it is only instantiated in this class. // </summary> private ILogger iLogger; /// <summary> // log factory object in singleton mode // </summary> public static LoggerFactory Instance {get {if (instance = null) {lock (lockObj) {if (instance = null) {instance = new LoggerFactory () ;}} return instance ;}} # endregion # region ILogger member public void Logger_Debug (string message) {if (level <= Level. DEBUG) iLogger. logger_Debug (message);} public void Logger_Info (string message) {if (level <= Level. INFO) iLogger. logger_Info (message);} public void Logger_Warn (string message) {if (level <= Level. WARN) iLogger. logger_Warn (message);} public void Logger_Error (Exception ex) {if (level <= Level. ERROR) iLogger. logger_Error (ex);} public void Logger_Fatal (string message) {if (level <= Level. FATAL) iLogger. logger_Fatal (message) ;}# endregion}View Code
After the program is running, we observe the root directory of the sdcard. The log already exists.
After the log Content is formatted, it is clear.
From the figure above, we can see that our log level Info and Error are also reflected.