I have installed Elmah for MVC using NuGet, I'am able to login with success error in the db.
The only problem is that I cannot access the /elmah URL to access the Error Log Page.
Here part of my configuration, could you please point out if I have any misconfiguration?
Thanks
ERROR
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
In my web.config:
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
<add key="elmah.mvc.requiresAuthentication" value="true" />
<add key="elmah.mvc.allowedRoles" value="Administrator" />
<add key="elmah.mvc.route" value="elmah" />
</appSettings>
In global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("elmah.axd");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
(This is all from the documentation/getting started)
You don't need the following line:
routes.IgnoreRoute("elmah.axd");
The next line takes care of it.
Everything you need to configure is in your web.config file. Something like:
<elmah>
<security allowRemoteAccess="yes" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="mySqlConnString" />
</elmah>
<location path="elmah.axd">
<system.web>
<authorization>
<allow roles="Administrator" />
<deny users="*" />
</authorization>
</system.web>
</location>
Should get you going.
Just in case anyone comes across the same issue I had.
This was my code, which is wrong:
<elmah>
<security allowremoteAccess="true" />
</elmah>
The issue was the r in allowremoteAccess, it was in lower case, when it should have been upper-case!
Correct code:
<elmah>
<security allowRemoteAccess="true" />
</elmah>
Even though I had added the remote access to my web.config:
<add key="elmah.mvc.allowedRoles" value="adminrole" />
<elmah>
<security allowRemoteAccess="true" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection" />
</elmah>
I had to edit Elmah.Athz.config on the server and add the role I wanted to give access to elmah. I had to add ^adminrole
Related
I am trying to wire-up serilog using the app settings package and when I run my program Serlog is complaining it cannot find the RollingFile assembly. I did not have this problem using a single sink but multiple it giving me fits:
<!-- Serilog Configuration -->
<add key="serilog:using:Email" value="Serilog.Sinks.Email" />
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<!-- Configure Serilog Email Sink -->
<add key="serilog:write-to:Email"/>
<add key="serilog:write-to:Email.mailServer" value="***" />
<add key="serilog:write-to:Email.toEmail" value="***" />
<add key="serilog:write-to:Email.fromEmail" value="***" />
<add key="serilog:write-to:Email.mailSubject" value="Notification" />
<add key="serilog:write-to:Email.restrictedToMinimumLevel" value="Debug" />
<!-- Configure Serilog RollingFile Sink -->
<add key="serilog:write-to:RollingFile" />
<add key="serilog:write-to:RollingFile.restrictedToMinimumLevel" value="Debug" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Logs\comply360-user-mgmt-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] [{CorrelationId}] {Message}{NewLine}{Exception}" />
You have to use prefix in web.config and in Configuration like this
Web.Config
<!-- Serilog Configuration -->
<add key="email:serilog:using:Email" value="Serilog.Sinks.Email" />
<add key="file:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<!-- Configure Serilog Email Sink -->
<add key="email:serilog:write-to:Email"/>
<add key="email:serilog:write-to:Email.mailServer" value="***" />
<add key="email:serilog:write-to:Email.toEmail" value="***" />
<add key="email:serilog:write-to:Email.fromEmail" value="***" />
<add key="email:serilog:write-to:Email.mailSubject" value="Notification" />
<add key="email:serilog:write-to:Email.restrictedToMinimumLevel" value="Debug" />
<!-- Configure Serilog RollingFile Sink -->
<add key="file:serilog:write-to:RollingFile" />
<add key="file:serilog:write-to:RollingFile.restrictedToMinimumLevel" value="Debug" />
<add key="file:serilog:write-to:RollingFile.pathFormat" value="C:\Logs\comply360-user-mgmt-{Date}.txt" />
<add key="file:serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] [{CorrelationId}] {Message}{NewLine}{Exception}" />
Startup.cs
Log.Logger = new LoggerConfiguration()
.ReadFrom
.AppSettings("email")
.ReadFrom
.AppSettings("file")
.CreateLogger()
figured it out. i had to remove the rolling file using statement.
I've asked this on the elmah group in google groups but am still mystified so I thought I'd throw it out to a larger pool. Here's the original note with my code as it currently stands.
I have ELMAH set up on my MVC 5 application and I want to use a custom email service to send email instead of using the smtp send that is set up by default. (This is an organizational preference.) I've been poking around at it but can't figure out how to do this. My service call is working just fine, but I can't hook it up so that it runs when there is an exception. Any assistance will be greatly appreciated.
What I've got set up is the following:
Web.config (removed non-elmah-related lines):
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
</configSections><appSettings>
...
<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
<add key="elmah.mvc.requiresAuthentication" value="false" />
<add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
<add key="elmah.mvc.allowedRoles" value="*" />
<add key="elmah.mvc.allowedUsers" value="*" />
<add key="elmah.mvc.route" value="elmah" />
</appSettings>
<connectionStrings>
<add name="BFRDPDB" providerName="System.Data.SqlClient" connectionString="blahblah;" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<customErrors mode="Off" defaultRedirect="~/Home/Error">
<error statusCode="404" redirect="~/Home/Error" />
</customErrors>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
</system.webServer>
<elmah>
<security allowRemoteAccess="false" />
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionStringName="BFRDPDB"
applicationName="FarmAnswers.org" />
</elmah>
<location path="elmah">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD"
path="elmah.axd"
type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<authorization>
<allow roles="SuperAdmin" />
<deny users="*" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH"
verb="POST,GET,HEAD"
path="elmah"
type="Elmah.ErrorLogPageFactory, Elmah"
preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
</configuration>
And my ErrorMailModule.cs file looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
namespace BFRDP.Infrastructure
{/// <summary>
/// Summary description for ErrorMailModule
/// </summary>
public class ErrorMailModule : Elmah.ErrorMailModule
{
public ErrorMailModule()
{
}
protected override void SendMail(MailMessage mail)
{
if (mail == null)
throw new ArgumentNullException("mail");
// Code to send email here through internal provider
}
}
}
My error is indeed logged in the database, but the code to send the email is never reached.
What am I doing wrong?
Added the errorMail settings in the section of my web.config and the app was able to get to my custom module:
<elmah>
<security allowRemoteAccess="false" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="BFRDPDB" applicationName="FarmAnswers.org" />
<errorMail
from="elmah#example.com"
to="admin#example.com"
subject="..."
priority="Low"
async="true"
smtpPort="25"
smtpServer="smtp.example.com"
useSsl="true"
userName="johndoe"
password="secret"
noYsod="true" />
</elmah>
I have a simple app: http://locahost/ITSuggestionBox that takes a user to a comment box and they submit that data, which is stored in a database. I have another page: .../Details where the representation of that data appears. I have added authentication so that those going to that URL need to be authenticated against AD otherwise they are redirected to the login page. The problem is, when I go to my app now, it is redirected to: http://localhost/ITSuggestionBox/Account/Login?ReturnUrl=%2fITSuggestionBox and throws the following error:
Format of the initialization string does not conform to specification starting at index 0.
Line 32: using (var context = new UsersContext())
Line 33: {
Line 34: if(!context.Database.Exists())
Line 35: {
Line 36: // Create the SimpleMembership database without Entity Framework migration schema
Source File: c:\Dev\_MyProjects\SuggestionBox\SuggestionBox\Filters\InitializeSimpleMembershipAttribute.cs Line: 34
Here is my web.config:
<connectionStrings>
<add name="CommentDbContext" connectionString="Data Source=DBServer;Initial Catalog=DBName;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="ADConnectionString" connectionString="LDAP://testdomain.test.com/CN=Users,DC=testdomain,DC=test,DC=com" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5"><assemblies><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></assemblies></compilation>
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms name="ADAuthCookie" loginUrl="~/Account/Login" timeout="15" slidingExpiration="false" protection="All" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<membership defaultProvider="MY_ADMembershipProvider">
<providers>
<clear />
<add name="MY_ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
Is there a reason why the app is trying to use SimpleMembership database? I have used a number of pages but seem to be stuck right now:
asp.net forms authentication with active directory
ASP.NET MVC - Authenticate users against Active Directory, but require username and password to be inputted
I´m following this tutorial:http://robbincremers.me/2012/02/22/using-windows-azure-access-control-service-to-provide-a-single-sign-on-experience-with-popular-identity-providers/#comment-469
Using this guide or others, when uncomment the authentication forms in the web config for using the custom html login form downloaded from windows azure access control portal, I get an 500 Internal Server Error. What could be wrong?
It is just adding authentication form
<location path="FederationMetadata">
<system.web>
<customErrors mode="Off"/>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<system.web>
<httpRuntime requestValidationMode="2.0"/>
<!-- <authorization>
<deny users="?" />
</authorization>-->
<authentication mode="None" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<!--Commented out by FedUtil-->
<authentication mode="Forms"><forms loginUrl="~/Federation/Login.html" timeout="2880" /></authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<httpModules>
<add name="WSFederationAuthenticationModule" type="Microsoft.IdentityModel.Web.WSFederationAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="SessionAuthenticationModule" type="Microsoft.IdentityModel.Web.SessionAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
</system.web>
That's because the authentication element is defined twice. Only one can exist in the web.config. On line 14 you have this:
<authentication mode="None" />
On line 21 you have this:
<authentication mode="Forms"><forms loginUrl="~/Federation/Login.html" timeout="2880" /></authentication>
Remove or comment out one of these lines to fix the issue.
I am playing around with Elmah to see if I want to use that as my error handling solution. I installed it, hard coded an exception into my page, hit the page wholla! Got my email, everything is happy. However, when I added the customError node to my web.config to redirect to a friendly error page, the email was not sent but I was redirected to my friendly error page.
Strangely, when I browsed to a page that doesn't exist on my site, I was redirected to home (as I set in my customErrors) but I DID receive the email...that could be problematic as I don't want to get a billion emails when people hit my site and add "whatever.php" to the end of the url.
So I have two questions: 1) why would the exception that is being thrown NOT send me an email and 2) how can I tell Elmah NOT to send me emails for 404s?
You can filter things with elmah like such in your Global.asax.cs:
//ELMAH Filtering
protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
FilterError404(e);
}
protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
FilterError404(e);
}
//Dimiss 404 errors for ELMAH
private void FilterError404(ExceptionFilterEventArgs e)
{
if (e.Exception.GetBaseException() is HttpException)
{
HttpException ex = (HttpException)e.Exception.GetBaseException();
if (ex.GetHttpCode() == 404)
{
e.Dismiss();
}
}
}
So add the call to FilterError404 to any part of the filtering. The above example will have it filter 404 for both ErrorLog and Email. Also check out:
http://code.google.com/p/elmah/wiki/ErrorFiltering
You can also do Filtering By Source as described in the link:
<elmah>
...
<errorFilter>
<test>
<and>
<equal binding="HttpStatusCode" value="404" type="Int32" />
<regex binding="FilterSourceType.Name" pattern="mail" />
</and>
</test>
</errorFilter>
</elmah>
Check web.config:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
</configSections>
<appSettings />
<!-- ELMAH: Configuration -->
<elmah>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah.Sql" />
<errorMail defaultCredentials="true" from="someuser#example.com" to="someuser#example.com, someuser2#example.com" subject="Error (STAGING): {0}" async="true" smtpPort="25" smtpServer="192.168.1.1" userName="smtpUserName" password="smtpPassword" />
</elmah>
<connectionStrings>
<add name="Elmah.Sql" connectionString="Data Source=192.168.1.1;database=DBName;integrated security=false;User ID=MyUserName;Password=MyPassword" />
</connectionStrings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="someuser#example.com">
<network defaultCredentials="true" host="192.168.1.1" port="25" userName="smtpUserName" password="smtpPassword" />
</smtp>
<!-- Use this setting for development
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
</smtp>
-->
</mailSettings>
</system.net>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
...........................
</assemblies>
</compilation>
.......................
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
-->
<customErrors mode="RemoteOnly" defaultRedirect="~/Home/MyErrorPage" />
.............................
<httpHandlers>
..............................
<!--ELMAH-->
<add verb="POST,GET,HEAD" path="/MyErrorPage/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<httpModules>
........................
<!-- ELMAH: Logging module -->
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
.............................
<!-- ELMAH-->
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</modules>
<handlers>
..............
<!--ELMAH-->
<add name="Elmah" verb="POST,GET,HEAD" path="/MyErrorPage/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</handlers>
</system.webServer>
..................
</configuration>
FYI: NuGET Package also available as explained by Scott Hanselman:
http://www.hanselman.com/blog/NuGetPackageOfTheWeek7ELMAHErrorLoggingModulesAndHandlersWithSQLServerCompact.aspx