如何将nlog消息存储到sqlite中?使用xamarin.ios

时间:2014-02-04 12:33:03

标签: ios sqlite xamarin nlog

在xamarin .ios我正在使用nlog,因为如果我创建xls文件&存储消息它的工作正常,但我试图存储到sqllite它不工作。我创建了一个名为ErroLog的表,在4列创建.database存储到MyDocument文件夹。你能告诉我下面的代码,有什么问题?

private void InitializeNLog()
{
    var saveDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    var documents = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    _pathToDatabase = Path.Combine(saveDir, "db_sqlite-net.db");

    // Create the database and a table to hold Person information.
    using (var conn= new SQLite.SQLiteConnection(_pathToDatabase))
    {
        conn.CreateTable<ErrorLog>();
    }

    // this coding insert is working but error value not updated
    // var errorlog = new ErrorLog { Time = "${longdate}", Level = "${level}", Lessage ="${message}", Logger = "${logger}" };
    //      
    // using (var db = new SQLite.SQLiteConnection(_pathToDatabase ))
    // {
    //     db.Insert(errorlog);
    // }

    DatabaseTarget target = new DatabaseTarget();

    target.DBProvider = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139";
    target.ConnectionString = "Data Source="+_pathToDatabase+";Version=3;";
    target.CommandText = "insert into ErrorLog( Time,Level,Lessage,Logger) values(@Time,@Level,@Lessage,@Logger)";
    // target.CommandText = "insert into ErrorLog( Time,Level,Lessage,Logger) values

    target.KeepConnection = true;

    target.Parameters.Add(new DatabaseParameterInfo("@Time","${longdate}"));
    target.Parameters.Add(new DatabaseParameterInfo("@Level","${level}"));
    target.Parameters.Add(new DatabaseParameterInfo("@Lessage","${message}"));
    target.Parameters.Add(new DatabaseParameterInfo("@Logger","${logger}"));

    SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Info);

    _logger = LogManager.GetCurrentClassLogger ();
    _logger.Info ("Sample informational message");

    // SimpleConfigurator.ConfigureForTargetLogging(
    //     new FileTarget()
    //     {
    //         FileName = Path.Combine(saveDir,"${shortdate}.csv"),
    //         Layout = new CsvLayout()
    //         {
    //             Columns =
    //             {
    //                 new CsvColumn"Time", "${longdate}"),
    //                 new CsvColumn("Level", "${level}"),
    //                 new CsvColumn("Lessage", "${message}"),
    //                 new CsvColumn("Logger", "${logger}")
    //             },
    //         }
    //     },
    //     LogLevel.Info);
}

1 个答案:

答案 0 :(得分:0)

每次初始化时,您很可能会覆盖数据库。试试这个:

if (!File.Exists(_pathToDatabase)) {
  using (var conn= new SQLite.SQLiteConnection(_pathToDatabase)) {
    conn.CreateTable<ErrorLog>();
  }
}