Skip to content

Instantly share code, notes, and snippets.

View hgmauri's full-sized avatar

Henrique Mauri hgmauri

View GitHub Profile
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(wt => wt.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Properties:j}{NewLine}{Exception}"))
.CreateLogger();
var logLevel = builder.Environment.IsProduction() ? LogEventLevel.Warning : LogEventLevel.Debug;
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(wt => wt.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Properties:j}{NewLine}{Exception}", restrictedToMinimumLevel: logLevel))
.CreateLogger();
using Serilog;
using Serilog.BestPractices.Extensions;
try
{
var builder = WebApplication.CreateBuilder(args);
builder.AddSerilogApi();
builder.Services.AddControllers();
[Route("api/[controller]")]
public class SampleController : Controller
{
private readonly OpenAIAPI _chatGpt;
public SampleController(OpenAIAPI chatGpt)
{
_chatGpt = chatGpt;
}
public static class ChatGptExtensions
{
public static WebApplicationBuilder AddChatGpt(this WebApplicationBuilder builder, IConfiguration configuration)
{
var key = configuration["ChatGpt:Key"];
var chat = new OpenAIAPI(key);
builder.Services.AddSingleton(chat);
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Sample.ChatGpt.WebApi.Core.Extensions;
using Sample.ChatGpt.WebApi.Core.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.AddSerilog(builder.Configuration, "Sample ChatGPT");
builder.AddChatGpt(builder.Configuration);
builder.Services.AddRouting(options => options.LowercaseUrls = true);
var builder = WebApplication.CreateBuilder(args);
builder.AddSerilog(builder.Configuration, "Sample Scrutor");
Log.Information("Starting API");
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddClassesMatchingInterfaces(nameof(Sample));
services.Scan(scan => scan
.FromAssembliesOf(typeof(IUserService))
.AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
.AsSelf()
.WithScopedLifetime());
services.Scan(scan => scan
.FromAssembliesOf(typeof(IUserService))
.AddClasses(classes => classes.HavingAttribute<CustomServiceAttribute>())
.AsImplementedInterfaces()
.WithScopedLifetime());
services.AddScoped<ICustomerService, CustomerService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IBusinessService, BusinessService>();