Question: Is there a way to dump the generated sql to the Debug log or something? I'm using it in a winforms solution so the mini-profiler idea won't work for me.

Answer: 
Try [Dapper.Logging](https://github.com/hryz/Dapper.Logging/tree/71f4f3de06b95f131e84171698e0d3ed7cb52f83).
-----------------------------------------------------------------------------------------------------------

You can get it from NuGet. The way it works is you pass your code that creates your actual database connection into a factory that creates wrapped connections. Whenever a wrapped connection is opened or closed or you run a query against it, it will be logged. You can configure the logging message templates and other settings like whether SQL parameters are saved. Elapsed time is also saved.

In my opinion, the only downside is that the documentation is sparse, but I think that's just because it's a new project (as of this writing). I had to dig through the repo for a bit to understand it and to get it configured to my liking, but now it's working great.

From the documentation:

> 
> The tool consists of simple decorators for the `DbConnection` and
> `DbCommand` which track the execution time and write messages to the
> `ILogger<T>`. The `ILogger<T>` can be handled by any logging framework
> (e.g. Serilog). The result is similar to the default EF Core logging
> behavior.
> 
> 
> The lib declares a helper method for registering the
> `IDbConnectionFactory` in the IoC container. The connection factory is
> SQL Provider agnostic. That's why you have to specify the real factory
> method:
> 
> 
> 
> ```
> services.AddDbConnectionFactory(prv => new SqlConnection(conStr));
> 
> ```
> 
> After registration, the `IDbConnectionFactory` can be injected into
> classes that need a SQL connection.
> 
> 
> 
> ```
> private readonly IDbConnectionFactory _connectionFactory;
> public GetProductsHandler(IDbConnectionFactory connectionFactory)
> {
>     _connectionFactory = connectionFactory;
> }
> 
> ```
> 
> The `IDbConnectionFactory.CreateConnection` will return a decorated
> version that logs the activity.
> 
> 
> 
> ```
> using (DbConnection db = _connectionFactory.CreateConnection())
> {
>     //...
> }
> 
> ```
> 
>
This is not exhaustive and is essentially a bit of hack, but if you have your SQL and you want to initialize your parameters, it's useful for basic debugging. Set up this extension method, then call it anywhere as desired.

```
public static class DapperExtensions
{
    public static string ArgsAsSql(this DynamicParameters args)
    {
        if (args is null) throw new ArgumentNullException(nameof(args));
        var sb = new StringBuilder();
        foreach (var name in args.ParameterNames)
        {
            var pValue = args.Get<dynamic>(name);

            var type = pValue.GetType();

            if (type == typeof(DateTime))
            sb.AppendFormat("DECLARE @{0} DATETIME ='{1}'\n", name, pValue.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            else if (type == typeof(bool))
                sb.AppendFormat("DECLARE @{0} BIT = {1}\n", name, (bool)pValue ? 1 : 0);
            else if (type == typeof(int))
                sb.AppendFormat("DECLARE @{0} INT = {1}\n", name, pValue);
            else if (type == typeof(List<int>))
                sb.AppendFormat("-- REPLACE @{0} IN SQL: ({1})\n", name, string.Join(",", (List<int>)pValue));
            else
                sb.AppendFormat("DECLARE @{0} NVARCHAR(MAX) = '{1}'\n", name, pValue.ToString());
        }
        return sb.ToString();
    }
}

```

You can then just use this in the immediate or watch windows to grab the SQL.