I am working on minimal APIs and not quite sure how to add Azure App Insights logging.
Could someone please point me to any documentation?
In the older version of .netcore API we could do it in the program file by calling CreateWebHostBuilder and passing the App Insight values. But I can see we have WebApplicationBuilder in the minimal API, so not sure how to configure logging to Azure.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging=> {
logging.AddApplicationInsights("your_insturmentation_key");
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace); #you can set the logLevel here
});
}
Thanks in advance.
You can add Application Insight by adding the following line in your Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// ....
services.AddApplicationInsightsTelemetry();
}
Further reading: Application Insights for ASP.NET Core applications
Related
I have asp.net core 3.1 application. These are my settings for logging:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddAzureWebAppDiagnostics();
logging.AddConsole();
logging.AddDebug();
})
.UseStartup<Startup>();
And in code I use this _logger.LogInformation(). I can see logs in console when I run the app locally, but the question is how I can see it on my app service on Azure Portal?
I'm developing a .NET Core 3.0 web application and publishing it as an Azure App Service.
In one of the controller methods, I do this:
System.Diagnostics.Trace.TraceError("If you're seeing this, something wonderful happened");
Then I go in the Azure App Service - App Service Logs and enable "Application Logging". Next, I go in "Log stream", where I'm supposed to be able to see live debug logging, but I don't see anything.
Please advice!
...
Edited: I changed my Program.cs file a bit but it still doesn't work. Here's my Program.cs file:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
})
.UseStartup<Startup>();
}
You need to change your logging to something like explained here. AFAIK System.Diagnostics.Trace is not really supported in ASP.NET Core.
This seemed to work:
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
//logging.ClearProviders();
//logging.AddConsole();
logging.AddApplicationInsights("Application insights Instrumentation Key");
})
.UseStartup<Startup>();
Using .NET Core 2 in Azure App Service. Don't see my logs. In the actual application, the logging code looks like this:
using System.Diagnostics;
...
Trace.WriteLine("Logging works");
I expected to see the Trace logs in the Log Stream, but I don't. I do see general API logs. What am I doing wrong? My config below:
I had the same issue for .NET core azure web app: Trace.WriteLine method does not work(not write message to Application logs) for .NET core, but work for .NET framework , and I find the issue about that.
As a workaround for .NET core web application, I suggest you can use ILogger, which can write message to Application Logs.
In Startup.cs -> Configure method, re-write Configure method like below:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//your other code
//add the following 2 lines of code.
loggerFactory.AddConsole();
loggerFactory.AddDebug();
app.UseStaticFiles();
//your other code
}
Then in HomeController.cs, add the following code:
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HomeController>();
}
public IActionResult Index()
{
_logger.LogInformation("this is a information from ILogger...");
return View();
}
//other code
}
After publish to azure, and configure the Application Logging -> run the azure web app -> you can see the message is displayed in Application Logs:
I'm creating a new Azure Functions with a Queue trigger. A key requirement is for me to use my existing class libraries that I created in my ASP.NET Core app so that I can access my Repository methods. I also have my own clients that handle communication with some third party services.
I need help with creating instances of my clients and passing configuration to them which is IConfiguration.
Here's what my Startup.cs looks like in my Azure Functions project:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Infrastructure.Clients;
using MyApp.Infrastructure.Interface;
using MyApp.Infrastructure.Repositories;
[assembly: FunctionsStartup(typeof(MyTestFunction.Startup))]
namespace MyTestFunction
{
public class Startup : FunctionsStartup
{
public IConfiguration Configuration { get; }
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton(new MyApp.Infrastructure.Clients.MyClient123(Configuration));
builder.Services.AddTransient<ICommunicationsRepository, CommunicationsRepository>();
}
}
}
In my ASP.NET Core app's Startup.cs, I do have a constructor that handles the configuration -- see below -- but not sure how to handle this Azure Functions.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
Looks like this is not a recommended approach:
https://github.com/Azure/azure-functions-host/issues/4464#issuecomment-513017446
I've decided to update my class libraries in my ASP.NET Core app so that I use Azure KeyVault in both my API and Azure Functions apps.
I have an Azure web app with several web jobs in the back-end. Up until now, I have been using traditional .Net to create these web jobs. One of the core aspects of these web jobs is that they all access global configuration properties defined in the Azure portal, as shown below:
And I reference these settings in my traditional .Net app like so:
var appKey = ConfigurationManager.AppSettings["RingCentral_AppKey"];
I am now starting to transition my web jobs to .Net Core. However, the "best practice" for for managing/retrieving app settings like this seems to be in an appsettings.json file within each web job:
And I reference my settings in my .Net Core app like so:
class Program
{
public static IConfiguration StaticConfig { get; set; }
static void Main(string[] args)
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
});
builder.ConfigureAppConfiguration((hostContext, config) =>
{
var conf = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();
config.AddConfiguration(conf);
});
var host = builder.Build();
using (host)
{
host.Run();
}
}
}
public class Functions
{
IConfiguration configuration;
public Functions(IConfiguration _configuration)
{
configuration = _configuration;
}
[NoAutomaticTrigger]
public void DoTrigger(ILogger logger, [Queue("messagestarterqueue")] ICollector<string> outputQueueMessage)
{
var appKey = configuration["RingCentral_AppKey"];
}
}
That's all good, but I have to have a separate appsettings.config file in each separate project, each with the same settings repeated. Is there a way to "centralize" theses settings for my .Net Core web jobs? Either create a "global" appsettings.config file that my Azure web app can reference? Or, is there some way that each .Net Core app can reference the "old style" application settings that are currently being used by the traditional .Net web jobs?
Allow me to suggest a bit different approach.
IMHO, the best way you can do it is by utilizing the Azure App Configuration service.
(Check it on GitHub)
It will allow you to call an API to get the configured values, while you can manage it conveniently through the portal.
Actually you could use Environment.GetEnvironmentVariable(your settings key) to get the settings.
After you set the settings in Configuration, they will be saved as environment variables, you could check them in https://yousitename.scm.azurewebsites.net/Env.cshtml.
So of course yo could get them GetEnvironmentVariable, it will show as below pic.