Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:15:30+00:00 2026-06-17T20:15:30+00:00

I have a logging interface which I extend with some helpful extension methods as

  • 0

I have a logging interface which I extend with some helpful extension methods as to make it possible for me to pass in a format and a list of arguments to avoid having to use string format every time a call the method. (it also help me follow FXCops culture info rules)

So I can call:

logger.Debug("Created {0} with id {1}",typeof(MyObject).Name ,myObject.Id);

Instead of:

logger.Debug(string.Format("Created {0} with id {1}", typeof(MyObject).Name, myObject.Id));

I now found myself in a bit of a tricky situation because it would be immensely helpful to also get some info in the logs about where the logging was written such as the file, method, and line number. This could be achieved with the neat [CallerMemberName], [CallerFilePath], and [CallerLineNumber] attribute.

logger.Debug("Created {0} with id {1}", typeof(MyObject).Name, myObject.Id);

would then give me a log entry such as:

“MyObjectProvider.cs, Provide, line:50 | Created MyObject with id 1564”

The issue here is that the method signature would look like this:

public static void Debug(this ILogger logger, string format [CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = 0, params object[] args)

and that is not possible because the [Caller*] attributes makes the parameters optional and that doesn’t work with the args parameter.

I also tried to make multiple implementations with fixed amount of strings as parameters like this:

public static void Debug(this ILogger logger, string format [CallerMemberName] string callerMemberName = "",string arg, string arg2 , ...etc... , [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = 0)

but then I get compiler errors saying the “The call is ambiguous between the following methods or properties”

I have almost given up on this issue now but I thought to myself, “Maybe SO can find a solution for me”. So here it is… Is it possible to use both params object[] args and [CallerFilePath] in any way or is there another way to get the intended result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:15:31+00:00Added an answer on June 17, 2026 at 8:15 pm

    You can’t combine the two in a method signature. What you could do is one or the other and pass in null to where you need optional parameters, would this work for you?

    Foo(s, null);
    public void Foo(string s, params string[] sArray)
    {
    
    }
    
    Foo(new string[] {""});
    private static void Foo(string[] sArray,  string s = "")
    {
    }
    

    OR

    Why not use a class which handles your formatting and make that optional?

    public class LogArgs
    {
      private string _formatString;
      private string[] _args;
      public LogArgs(string formatString, params string[] args)
      {
        _formatString = formatString;
        _args = args;
      }
      public override string ToString()
      {
        return string.Format(_formatString, _args);
      }
    }
    
    public void Foo(string mandatory, LogArgs optionalParam = null)
    {
      //Do Stuff
    }
    
    Foo("", new LogArgs("{0} is formatted", ""));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make a logging application and I have a LogEvent object with some string
In my application, I have a 'logging' window, which shows all the logging, warnings,
I have some python module, which has a class ModuleClass and I can't modify
I have a little Android project going on which involves some IPC where client
I have a bunch of projects which declare some spring bean files. I would
I have some methods advised by two aspects, one is using the spring AOP
I have a simple WCF logging service.. namespace WCFServiceLibrary { [ServiceContract] public interface ILog
I have a base controller class that exposes some logging functionality to it's child
I have a python-based daemon that provides a REST-like interface over HTTP to some
I need to shutdown alot of differently implemented threads which only have some Shutdown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.