AwsMetadataHelper is a .NET library that provides easy access to AWS EC2 instance metadata and enriches logging with EC2 instance information. This library is designed to help developers retrieve instance metadata and integrate it seamlessly into their logging infrastructure.
- Retrieve EC2 instance metadata, such as instance ID and instance type.
- Enrich logging messages with EC2 instance information.
- Easy integration with popular logging frameworks.
To install the AwsMetadataHelper package, run the following command in the NuGet Package Manager Console:
Install-Package AwsMetadataHelper
Alternatively, you can add it to your project file:
<PackageReference Include="AwsMetadataHelper" Version="1.0.0" />
Here's an example of how to use AwsMetadataHelper to retrieve EC2 instance metadata:
using System;
using AwsMetadataHelper;
class Program
{
static async Task Main(string[] args)
{
string instanceId = await AwsMetadataHelper.GetInstanceIdAsync();
Console.WriteLine($"EC2 Instance ID: {instanceId}");
}
}
To enrich your logging messages with EC2 instance information, follow these steps:
- Add the logging extension method in your Startup class:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using AwsMetadataHelper;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAwsMetadataLogging();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
- Configure logging to use AwsMetadataHelper:
using Microsoft.Extensions.Logging;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddAwsMetadataLogger();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Push to the branch.
- Open a pull request.
If you encounter any issues or have any questions, feel free to open an issue on GitHub or contact us at support@example.com.
This project is licensed under the MIT License. See the LICENSE file for details.