Copied to clipboard

Flag this post as spam?

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


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

    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 1593 posts 7151 karma points MVP 6x 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 1136 posts 2523 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;
    
Please Sign in or register to post replies

Write your reply to:

Draft