i'm trying create simple c# program in linqpad test dbcontext stuff. set connection datamodels assembly , config file. however, when trying run program receive message:
resolutionfailedexception: resolution of dependency failed, type = "clark.logging.ilogger", name = "(none)". exception occurred while: while resolving.
exception is: invalidoperationexception - current type, clark.logging.ilogger, interface , cannot constructed. missing type mapping?
at time of exception, container was:
resolving clark.logging.ilogger,(none)
so far, program simply:
void main() { var teststring = "testing"; teststring.dump(); // more code go here, use connection , context. }
this works if not select connection.
i believe datamodel assembly uses unity ioc custom logging setup. reason associating linqpad file connection causes error.
i may need provide more information here, can use connection?
unity doesn't find map, try construct type. in case fails since cannot construct interface.
with unity, can either configuration using:
container.registertype<interfacetype, concretetype>();
update#
kindly try below , see, if helps.
void main() { ilogger log = new logger(); var source = new subject<int>(); source.log(log, "sample") .subscribe(); source.onnext(1); source.oncompleted(); }
broadcasts log entry of configured listener/appender instances write if appropriate
- the log level
- the message log
an optional exception instance associated log. useful capturing stack traces.
public interface ilogger { void write(loglevel level, string message, exception exception); } public class logger : ilogger { public void write(loglevel level, string message, exception exception) { message.dump(level.tostring()); } }
public enum loglevel { /// /// lowest level, information considered verbose. /// verbose, /// /// second lowest level, tracing workflow in detail logging. /// trace, /// /// logging information may debugging problems. /// debug, /// /// non-critical error has occurred not interrupt application may degrade user experience. /// warn, /// /// interesting business or technical actions such process starting or request being made. /// info, /// /// errors need immediate attention. /// error, /// /// catastrophic failures. /// fatal }
extensions methods interface.
public static class loggerextensions { /// <summary> /// logs message exception fatal. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void fatal(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.fatal, formattedmessage, exception); } /// <summary> /// logs message exception fatal. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void fatal(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.fatal(null, format, args); } /// <summary> /// logs message exception error. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void error(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.error, formattedmessage, exception); } /// <summary> /// logs message exception error. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void error(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.error(null, format, args); } /// <summary> /// logs message exception info. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void info(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.info, formattedmessage, exception); } /// <summary> /// logs message exception info. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void info(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.info(null, format, args); } /// <summary> /// logs message exception warn. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void warn(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.warn, formattedmessage, exception); } /// <summary> /// logs message exception warn. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void warn(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.warn(null, format, args); } /// <summary> /// logs message exception debug. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void debug(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.debug, formattedmessage, exception); } /// <summary> /// logs message debug. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void debug(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.debug(null, format, args); } /// <summary> /// logs message exception trace, second lowest level. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void trace(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.trace, formattedmessage, exception); } /// <summary> /// logs message trace, second lowest level. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void trace(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.trace(null, format, args); } /// <summary> /// logs message exception verbose, lowest level. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="exception">the related <see cref="exception"/> message</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void verbose(this ilogger logger, exception exception, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var formattedmessage = format(format, args); logger.write(loglevel.verbose, formattedmessage, exception); } /// <summary> /// logs message verbose, lowest level. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="format">the message string format</param> /// <param name="args">the arguments message</param> //[stringformatmethod("format")] public static void verbose(this ilogger logger, string format, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); logger.verbose(null, format, args); } private static string format(string format, params object[] args) { if (args == null || args.length == 0) return format; return string.format(cultureinfo.currentculture, format, args); } /// <summary> /// logs entry method string "mytype.mymethod(1, abc)". /// ensure method being logged not in-lined compiler/jitter /// [methodimpl(methodimploptions.noinlining)] attribute. /// </summary> /// <param name="logger">the instance of logger log with</param> /// <param name="args">the arguments passed method</param> /// <remarks> /// <see cref="methodentry"/> logging method useful logging method has /// been entered , capturing arguments of method in log. /// <example> /// in example log method entry , arguments. /// <code> /// <![cdata[ /// [methodimpl(methodimploptions.noinlining)] /// public void myloggedmethod(string s, datetime datetime) /// { /// _logger.methodentry(s, datetime); /// //method body goes here... /// } /// /// ]]> /// </code> /// output may /// 2012-01-31 12:00 [ui] debug myloggedtype.myloggedmethod(a, 31/12/2001 13:45:27) /// </example> /// </remarks> public static void methodentry(this ilogger logger, params object[] args) { if (logger == null) throw new argumentnullexception("logger"); var stacktrace = new stacktrace(); var method = stacktrace.getframe(1).getmethod(); var type = method.declaringtype; var typename = string.empty; if (type != null) typename = type.name; string parenth = "()"; var parameterdefinitions = method.getparameters(); if (parameterdefinitions.length > 0) { if (args == null || args.length == 0) { parenth = "(...)"; } else { var values = string.join(", ", args); parenth = string.format(cultureinfo.currentculture, "({0})", values); } } logger.debug("{0}.{1}{2}", typename, method.name, parenth); } public static iobservable<t> log<t>(this iobservable<t> source, ilogger logger, string name) { return observable.using( () => logger.time(name), timer => observable.create<t>( o => { logger.trace("{0}.subscribe()", name); var subscription = source .do( => logger.trace("{0}.onnext({1})", name, i), ex => logger.trace("{0}.onerror({1})", name, ex), () => logger.trace("{0}.oncompleted()", name)) .subscribe(o); var disposal = disposable.create(() => logger.trace("{0}.dispose()", name)); return new compositedisposable(subscription, disposal); }) ); } public static idisposable time(this ilogger logger, string name) { return new timer(logger, name); } private sealed class timer : idisposable { private readonly ilogger _logger; private readonly string _name; private readonly stopwatch _stopwatch; public timer(ilogger logger, string name) { _logger = logger; _name = name; _stopwatch = stopwatch.startnew(); } public void dispose() { _stopwatch.stop(); _logger.debug("{0} took {1}", _name, _stopwatch.elapsed); } } }
Comments
Post a Comment