title: How to use ITranscriptLogger and TranscriptLoggerMiddleware to store chat transcript in cosmos DB
body: <p>I want to store the entire chat history in cosmos DB using ITranscriptLogger and TranscriptMiddelWare, but I am struggling to do so. I have read <a href="https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&amp;tabs=csharp" rel="nofollow noreferrer">this MS article</a>, but I want to store it in Cosmos DB and not Blob storage. Also I am trying to instantiate the transcript log in Startup.cs not in Bot.cs, and I have tried to implement it according to <a href="https://stackoverflow.com/questions/56016335/how-can-we-log-both-user-and-bot-messages-to-comos-db-in-microsoft-bot-framework">this answer</a> without any luck. That is, the transcript is not stored and there's no container in my Azure cosmos DB. I appreciate any help and feedback.</p>
  
  <p><strong>Code</strong>:</p>
  
  <p>I have created the TranscriptStore class and created and added the middleware as instructed in the referenced SO answer:</p>
  
  <p><strong>CosmosTranscriptStore.cs</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class CosmosTranscriptStore : ITranscriptLogger
      {
          private CosmosDbStorage _storage;
  
          public CosmosTranscriptStore(CosmosDbStorageOptions config)
          {
              _storage = new CosmosDbStorage(config);
          }
          public async Task LogActivityAsync(IActivity activity)
          {
              // activity only contains Text if this is a message
              var isMessage = activity.AsMessageActivity() != null ? true : false;
              if (isMessage)
              {
                  // Customize this to save whatever data you want
                  var data = new
                  {
                      From = activity.From,
                      To = activity.Recipient,
                      Text = activity.AsMessageActivity().Text,
                  };
                  var document = new Dictionary&lt;string, object&gt;();
                  // activity.Id is being used as the Cosmos Document Id
                  document.Add(activity.Id, data);
                  await _storage.WriteAsync(document, new CancellationToken());
              }
          }
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Startup.cs</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> public class Startup
      {
  
          private const string CosmosServiceEndpoint = "MyCosmosServiceEndpoint";
          private const string CosmosDBKey = "MyCosmosDBKey";
          private const string CosmosDBDatabaseName = "MyCosmosDBDatabaseName";
          private const string CosmosDBCollectionName = "Transcript-storage";
        
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;    
          }
          
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
              var config = new CosmosDbStorageOptions
              {
                  AuthKey = CosmosDBKey,
                  CollectionId = CosmosDBCollectionName,
                  CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
                  DatabaseId = CosmosDBDatabaseName,
              };
  
              var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));
  
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
              // Create the Bot Framework Adapter.
              services.AddSingleton&lt;IBotFrameworkHttpAdapter, AdapterWithErrorHandler&gt;();
              
              // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
              services.AddSingleton&lt;MainDialog&gt;();
              services.AddTransient&lt;IBot, WelcomeBot&lt;MainDialog&gt;&gt;();
  
              services.AddBot&lt;WelcomeBot&lt;MainDialog&gt;&gt;(options =&gt;
              {
                  var middleware = options.Middleware;
                  middleware.Add(transcriptMiddleware);
              });
  
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseHsts();
              }
  
              app.UseDefaultFiles();
              app.UseStaticFiles();
  
              app.UseMvc();
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>