{"id":308,"date":"2009-11-30T12:16:49","date_gmt":"2009-11-30T10:16:49","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=308"},"modified":"2009-11-30T12:16:49","modified_gmt":"2009-11-30T10:16:49","slug":"programmatically-check-log4net","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/programmatically-check-log4net","title":{"rendered":"Programmatically check Log4Net log"},"content":{"rendered":"<p>Today I needed to create unit test that checked if the NHibernate query was generated optimally.<\/p>\n<p>Good thing is that, in case of an inefficient query, NHibernate puts a warning using log4net:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nlog.Warn( &quot;firstResult\/maxResults specified with collection fetch; applying in memory!&quot; );\n<\/pre>\n<p>I wanted something like this:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;Test]\npublic void FindUsersBy_QueryWithLimit_LimitsOnSQLSide()\n{\n    using (LogChecker logChecker\n        = new LogChecker(&quot;NHibernate&quot;, Level.Warn))\n    {\n        \/\/ Execute query using NHibernate\n\n        \/\/ ....\n\n        Assert.IsEmpty(logChecker.Messages);\n    }\n}\n<\/pre>\n<p>The problem is that it&#8217;s not that easy to attach MemoryAppender for a duration of unit test to the specified logger.<\/p>\n<p>Anyway here&#8217;s the code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class LogChecker : IDisposable\n{\n    readonly Logger _logger;\n    readonly Level _previousLevel;\n    readonly MemoryAppender _appender = new MemoryAppender();\n\n    public LogChecker(string logName, Level levelToCheck)\n    {\n        _logger = (Logger)LogManager.GetLogger(logName).Logger;\n        _logger.AddAppender(_appender);\n        _previousLevel = _logger.Level;\n        _logger.Level = levelToCheck;\n    }\n\n    public List&lt;string&gt; Messages\n    {\n        get\n        {\n            return new List&lt;loggingEvent&gt;(_appender.GetEvents())\n                  .ConvertAll(x =&gt; x.RenderedMessage);\n        }\n    }\n\n    public void Dispose()\n    {\n        _logger.Level = _previousLevel;\n        _logger.RemoveAppender(_appender);\n    }\n};\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I needed to create unit test that checked if the NHibernate query was generated optimally. Good thing is that, in case of an inefficient query, NHibernate puts a warning using log4net: log.Warn( &quot;firstResult\/maxResults specified with collection fetch; applying in memory!&quot; ); I wanted something like this: &#x5B;Test] public void FindUsersBy_QueryWithLimit_LimitsOnSQLSide() { using (LogChecker logChecker [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[32,38,56],"class_list":["post-308","post","type-post","status-publish","format-standard","hentry","category-programming","tag-log4net","tag-nhibernate","tag-unit-testing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/308"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}