我正在寻找一个日志记录实用程序,如NLog,Log4Net等。这将允许我登录我的Xamarin.Andriod、Xamarin.IOS和一个Xamarin.PCL项目。到目前为止,我看到的所有记录器在PCL项目中都不受支持,原因有很多(大多数是关于文件IO的)。有没有什么解决方案可以支持跨平台的日志记录,包括PCL项目?如果没有,您是如何在PCL(设计模式等)中实现日志记录的?
谢谢
发布于 2014-03-18 04:22:35
如果没有PCL记录器,您可能希望使用依赖项注入。以下只是一个概念(尽管它确实可以工作)和两个示例实现(Android Log和SQLite数据库)。
接近安卓日志类的抽象接口:https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Core/Logging/ILogService.cs
特定于Android的实现,围绕日志类的包装器:https://github.com/sami1971/SimplyMobile/blob/master/Android/SimplyMobile.Android/Logging/LogService.cs
依赖于CRUD提供程序的数据库日志记录的PCL实现:https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Core/Data/DatabaseLog.cs
用于Android PCL兼容库的CRUD提供商包装器(适用于iOS、SQLite.Net.Async和WP8):https://github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/Data/SimplyMobile.Data.SQLiteAsync/SQLiteAsync.cs
ServiceStack.OrmLite的CRUD提供者包装器(适用于iOS和安卓):https://github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/SimplyMobile.Data.OrmLite/OrmLite.cs
在应用程序级别,使用IoC容器注册您想要使用的服务。示例是针对WP8的,但要将其用于iOS和安卓,您只需更改ISQLitePlatform即可。
DependencyResolver.Current.RegisterService<ISQLitePlatform, SQLitePlatformWP8>()
.RegisterService<IJsonSerializer, SimplyMobile.Text.ServiceStack.JsonSerializer>()
.RegisterService<IBlobSerializer>(t => t.GetService<IJsonSerializer>().AsBlobSerializer())
.RegisterService<ILogService>(t =>
new DatabaseLog(
new SQLiteAsync(
t.GetService<ISQLitePlatform>(),
new SQLiteConnectionString(
Path.Combine(ApplicationData.Current.LocalFolder.Path, "device.log"),
true,
t.GetService<IBlobSerializer>())
)));当然,使用Android日志包装器会简单得多,因为它没有任何依赖关系:
DependencyResolver.Current.RegisterService<ILogService, LogService>();发布于 2014-03-18 04:18:27
根本没有跨平台的解决方案。你可以通过使用服务来解决这个问题。因此,创建接口ILogging并描述日志记录所需的所有方法。然后实现日志记录,在每个平台上实现ILogging。之后,在安装的每个平台上注册它
Mvx.RegisterSingleton<ILogging >(new Logging());之后,您可以轻松地从核心项目访问它
Mvx.Resolve<ILogging>();https://stackoverflow.com/questions/22463418
复制相似问题