Configure Logging Provider for Asp.Net app (#157)
This commit is contained in:
Родитель
7af7f324f6
Коммит
d7e98f270a
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ServerlessLibrary.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -12,19 +13,21 @@ namespace ServerlessLibrary
|
|||
public interface ICacheService {
|
||||
IList<LibraryItemWithStats> GetCachedItems();
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/questions/44723017/in-memory-caching-with-auto-regeneration-on-asp-net-core
|
||||
public class CacheService:ICacheService
|
||||
{
|
||||
protected readonly IMemoryCache _cache;
|
||||
private IHostingEnvironment _env;
|
||||
private readonly ILibraryStore libraryStore;
|
||||
private static bool cosmosDBInitialized;
|
||||
private readonly ILogger logger;
|
||||
|
||||
public CacheService(IMemoryCache cache, IHostingEnvironment env, ILibraryStore libraryStore)
|
||||
public CacheService(IMemoryCache cache, IHostingEnvironment env, ILibraryStore libraryStore, ILogger<CacheService> logger)
|
||||
{
|
||||
this._cache = cache;
|
||||
this._env = env;
|
||||
this.libraryStore = libraryStore;
|
||||
this.logger = logger;
|
||||
InitTimer();
|
||||
}
|
||||
|
||||
|
@ -67,7 +70,10 @@ namespace ServerlessLibrary
|
|||
var items = await ConstructCache();
|
||||
_cache.Set<LibraryItemsResult>(ServerlessLibrarySettings.CACHE_ENTRY, new LibraryItemsResult() { Result = items, IsBusy = false });
|
||||
}
|
||||
catch { }
|
||||
catch(Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "Failed to load cache");
|
||||
}
|
||||
}
|
||||
private async Task<IList<LibraryItemWithStats>> ConstructCache()
|
||||
{
|
||||
|
|
|
@ -10,7 +10,8 @@ namespace ServerlessLibrary.Controllers
|
|||
[ApiController]
|
||||
public class MetricsController : ControllerBase
|
||||
{
|
||||
private ILogger<MetricsController> logger;
|
||||
private readonly ILogger<MetricsController> logger;
|
||||
|
||||
public MetricsController(ILogger<MetricsController> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
|
@ -27,11 +28,12 @@ namespace ServerlessLibrary.Controllers
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError("Unable to update download count. Exception: {0}", ex.ToString());
|
||||
this.logger.LogError(ex, "Unable to update download count");
|
||||
}
|
||||
|
||||
return new JsonResult(true);
|
||||
}
|
||||
|
||||
// PUT api/<controller>/sentiment
|
||||
[ProducesResponseType(typeof(bool), 200)]
|
||||
[HttpPut]
|
||||
|
@ -56,7 +58,7 @@ namespace ServerlessLibrary.Controllers
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError("Unable to update Sentiments. Exception: {0}", ex.ToString());
|
||||
this.logger.LogError(ex, "Unable to update sentiments");
|
||||
}
|
||||
|
||||
return new JsonResult(true);
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.ApplicationInsights;
|
||||
|
||||
namespace ServerlessLibrary
|
||||
{
|
||||
|
@ -20,6 +15,15 @@ namespace ServerlessLibrary
|
|||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseApplicationInsights()
|
||||
.UseStartup<Startup>();
|
||||
.UseStartup<Startup>()
|
||||
.ConfigureLogging(
|
||||
builder =>
|
||||
{
|
||||
builder.AddApplicationInsights();
|
||||
builder.AddFilter<ApplicationInsightsLoggerProvider>("ServerlessLibrary.Program", LogLevel.Information);
|
||||
builder.AddFilter<ApplicationInsightsLoggerProvider>("ServerlessLibrary.Startup", LogLevel.Information);
|
||||
builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.2.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.9.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="4.0.1" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче