Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig100 1078 posts 2366 karma points c-trib
    May 26, 2020 @ 14:32
    Craig100
    0

    Logging Namespace Issue

    Umb V8.6.1

    Using the following code based on the docs in a .core project I get two errors which I can't fathom out:-

    1. An object reference is required for the non-static field, method, or property 'ImportProperties._logger'
    2. 'ILogger' has no applicable method named 'Info' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

      using Umbraco.Core;
      using Umbraco.Core.Logging;
      
      
      namespace BSL8.Core.Services {
      
      
      public class ImportProperties {
      
      
      
      private readonly ILogger _logger;
      
      
      public ImportProperties(ILogger logger) {
          _logger = logger;
      }
      
      
      public static void GetPropertyList() {
      
      
          _logger.Info<ImportProperties>("Error Saving Property ID: {PropertyId}", 123);
      }
      
      }

      }

    Any clues as to why this doesn't work would be appreciated.

    / Craig

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    May 26, 2020 @ 15:24
    Nik
    0

    Hi Craig,

    Try taking "static" off of the GetPropertyList method. _logger isn't set if you call that method as you aren't instantiating the class to get ILogger passed in.

    Thanks,

    Nik

  • Craig100 1078 posts 2366 karma points c-trib
    May 26, 2020 @ 15:47
    Craig100
    0

    Thanks Nik,

    That cleared the 1st error, still leaving the 2nd error and generated a new error on the API that calls the method which is:-

    namespace BSL8.Core.Controllers.APIControllers
    {
    
        public class PropertyImportController : UmbracoApiController
        {
            [HttpGet]
            public IHttpActionResult GetAllProperties()
            {
    
                ImportProperties.GetPropertyList();
                // Gets a list of properties then iterates through it to update each property that's out of date.
    
                return Ok();
            }
        }
    }
    

    The error being (as expected) An object reference is required for the non-static field, method, or property 'ImportProperties.GetPropertyList()

    So ILogger still doesn't have an "Info" method. Maybe it's being overwritten by another ILogger in one of the other usings:-

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    using Umbraco.Core.Logging;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Net;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Linq;
    using System.Net.Mail;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.Composing;
    using System.Collections.Generic;
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies