0

It seems in my code Elmah and Nlog don't log all my Web API errors. Here is an example that all C# Web API developer know:

    [Authorize]
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }

When I go to my browser and GET http://localhost:55328/api/Values/5 I receive this message:

<Error>
<Message>Authorization has been denied for this request.</Message>
</Error>

Fine but when I go to http://localhost:55328/elmah.axd Elmah doesn't log this error. I also don't know how force the log with NLog. And yes Elmah and NLog are working fine when the error happens inside the body of a method.

Franckentien
  • 324
  • 6
  • 21
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • You need a filter; there are ones available already so no need to implement it yourself. – Casey Jun 30 '16 at 02:30
  • This shed some light on it. http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/5936867#5936867 – Hakunamatata Jun 30 '16 at 02:35
  • I already followed all instruction from this link Hakunamatata. The post is old and all these solution are not working. The problem with this post is that each time someone ask the question everybody respond with this link. Perhaps because one of the answer is from Atif Aziz. Check the answer is from 2011 and we are in 2016. Update questions and answers is something that is not managed by stackoverflow. – Bastien Vandamme Jun 30 '16 at 02:48
  • Ok, maybe [this](http://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2016/03/04/how-to-create-a-custom-action-filter-in-asp-net-mvc.aspx) article could help. – Jeroen Heier Jun 30 '16 at 04:31

0 Answers0