Categories: ASP.NETASP.NET Core

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

Page: 1 2 3 4 5

davidsky69

View Comments

Recent Posts

Behavioral Patterns – Template method

Template method – It means the program defines an algorithm skeleton in the superclass, which lets subclasses…

4 years ago

Behavioral Patterns – Strategy

Strategy – It allows the programs to define a family of an algorithm, put each…

4 years ago

Behavioral Patterns – State

State – This pattern can let the program’s object alter its behavior when its internal state changes,…

4 years ago

Behavioral Patterns – Observer

Observer – It lets the programs define a subscription mechanism to notify multiple objects, which means when…

4 years ago

Behavioral Patterns – Null Object

Null Object – Designed to act as a default value of an object. From Design Pattern –…

4 years ago

Behavioral Patterns – Memento

Memento – It allows the programs to save and restore the object’s previous state without revealing its…

4 years ago