1 year ago

#66639

test-img

Eric Brown - Cal

Vs2019 ,netcore api, can't get elmahcore working

I have a .netcore vs2019 webapi, with okta security set up and running.

I wanted to add elmah support, in particular the emails sent on errors.

I can't get it working. I figure it has to be something stupid that's staring me in the face and I'm just missing it.

I have a global exception filter and middle ware (to handle logging), though I've tried turning them off, no joy.

Elmah package 2.0.7 is installed

enter image description here

I have the following appsettings

"AppSettings": {
    "Environment": "Development",
    "EnvironmentProd": "Production",
    "ApplicationName": "StudentPortal4Api",
    **"ElmahEmailTo": "Brown.XXXXps.org,XXXX@gmail.com",
    "ElmahEmailFrom": "Dev_XXX@jXXXs.org",
    "SmtpServer": "smtp.jXXXs.org",
    "SmtpPort": "25",**
    "bUseOktaJwtSecurity": "false",
    "bUseHttps": "false",
    "bDoAdPassExpireDtInCdssTest": "false"

in startup.cs

using ElmahCore;
using ElmahCore.Mvc;
using ElmahCore.Mvc.Notifiers;

...

public void ConfigureServices(IServiceCollection services)

<at the very bottom, last thing registered>


            EmailOptions emailOptions = new EmailOptions
                                            {
                                                MailRecipient     = Configuration[ "AppSettings:ElmahEmailTo" ],
                                                MailSender        = Configuration[ "AppSettings:ElmahEmailFrom" ],
                                                SmtpServer        = Configuration[ "AppSettings:SmtpServer" ],
                                                SmtpPort          = Convert.ToInt32( Configuration[ "AppSettings:SmtpPort" ] ),
                                                MailSubjectFormat = "Application:StudentPortal4Api, Environment:Dev, ServerBoxName:fixme",
                                            };

            services.AddElmah( options =>
                               {
                                   //options.OnPermissionCheck = context => context.User.Identity.IsAuthenticated; // user must be authenticated to see elmah page.- EWB
                                   options.ApplicationName = Configuration[ "AppSettings:ApplicationName" ];
                                   options.Notifiers.Add( new ErrorMailNotifier( "ElmahEmail", emailOptions ) );
                               } );
        }

But when I run an api call that throws an exception, no email, and nothing at (Rootapiurl)\elmah

Am I missing something?

Edit to add COnfigure method

 public void Configure( IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory )//, ILogger log
        {
            //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            //loggerFactory.AddDebug();
            
            loggerFactory.AddLog4NetCustom();
            ILogger log = loggerFactory.CreateLogger( "Startup::Configure(...)" );
            log.LogTrace( "Startup::Configure(...)" );

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            if ( GetUseOktaSecurityVar() )
            {
                //enable JWTToken auth
                app.UseAuthentication();
            }
            
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
                             {
                                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                                 //c.RoutePrefix = string.Empty;
                             });

            app.UseRouting();

            if ( GetHttpsSecurityVar() )
            {
                //    // refers to "https_port" in appsettings.json to get port - EWB
                app.UseHttpsRedirection( ); //https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-5.0&tabs=visual-studio
                log.LogTrace( "************** HTTPS SECURITY ON **************" );
            }
            else
            {
                log.LogTrace( "************************************************" );
                log.LogTrace( "************** HTTPS SECURITY OFF **************" );
                log.LogTrace( "************************************************" );
            }

            log.LogTrace( "" );

            if ( bJwtOn )// flag is tied to other code up in up in configureServices
            {
                log.LogTrace( "************** JWT SECURITY ON **************" );// NOTE: This will not work unless you uncomment out the [Authorize] tag on SpBaseController - EWB
            }
            else
            {
                log.LogTrace( "**********************************************" );
                log.LogTrace( "************** JWT SECURITY OFF **************" );
                log.LogTrace( "**********************************************" );
            }

            app.UseSpaStaticFiles();

            // CORS: UseCors with CorsPolicyBuilder.
            app.UseCors("AllowSpecificOrigin");

            app.UseAuthorization();


            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSpa(spa =>
            {
                if (env.IsDevelopment())
                    spa.Options.SourcePath = "ClientApp";
                else
                    spa.Options.SourcePath = "dist";

                if (env.IsDevelopment())
                {
                    spa.UseVueCli(npmScript: "serve");
                }

            });

            //app.UseMiddleware<ResponseTimeMiddleware>();

            //app.ConfigureExceptionHandler( loggerFactory.CreateLogger( "ExceptionHandler" ) );

            app.UseElmah();// must be after all other handlers and middle ware for errors. -EWB
        }

c#

asp.net-core-webapi

elmah

0 Answers

Your Answer

Accepted video resources