Aspect-oriented programming in .NET Core

Step 6. Add the scope in the “Startup” file. Register “ILogRepository” and “LogRepository.”

Fig 5 Add the scope method about AOP
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
	options.CheckConsentNeeded = context => true;
	options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<DemoDbContext>(
	option => option.UseSqlServer(Configuration.GetConnectionString("DB_EntityString")));

    services.AddScoped(typeof(IEnterprise_MVC_CoreRepository), typeof(Enterprise_MVC_Repository));
    
    services.AddScoped(typeof(ILogRepository), typeof(LogRepository));

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

Step 7. Add the Logfile physical path in the “Startup” file.

Fig 6 Add the log file path
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
	app.UseDeveloperExceptionPage();
    }
    else
    {
	app.UseExceptionHandler("/Home/Error");
    }

    var path = Directory.GetCurrentDirectory();
    loggerFactory.AddFile($"{path}\\Logs\\Log.txt");

    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseMvc(routes =>
    {
	routes.MapRoute(
	    name: "default",
	    template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Step 8. Setting the program.cs file about the AOP.

Fig 7 Add configure logging in program.cs file

2 Comments

Leave a Reply