0

what is the best way how Sql statements generated by OrmLite can be logged into a Logging framework like NLog ? I know about the method GetLastSql() on the DbConnection but is there another way?

We want to use OrmLite within the data layer with the possibility of logging all generated sql of a Windows Forms application which has some years and where all the sql is written within the GUI. Performance is an issue so we decided to go with OrmLite (NO context approach)

nhaberl
  • 417
  • 1
  • 4
  • 17

1 Answers1

2

You have to create your own implementation of ProfiledDbConnection class writing log to NLog. Now when you create instanace of OrmLiteConnectionFactory class set connection filter, which you can do like that:

 var db = new OrmLiteConnectionFactory(
   "ConnectionString", true, 
   SqliteOrmLiteDialectProvider.Instance) {
   ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
};

Here you have src of ProfiledDbConnection https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/MiniProfiler/Data/ProfiledDbConnection.cs

Adam Łepkowski
  • 2,048
  • 1
  • 24
  • 41
  • 1
    Thank you, but this looks to complicated for just logging sql - we are using a generic wrapper (some kind of generic repository) around DbConnection which uses GetLastSql() method which is suggested here - http://stackoverflow.com/questions/11530943/servicestack-ormlite-sql-query-logging – nhaberl Aug 06 '13 at 07:18
  • I forgot about second possibility(The easiest). – Adam Łepkowski Aug 06 '13 at 07:29