log4net connection type invalid in AdoNetAppender - connection-string

I have implemented log4net ver 1.2.13.0 without major problems.(.Net ver 4.51 C#4)
I add an AdoNetAppender following documentation.
But here is my piece of code causing some troubles :
<appender name ="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value ="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
I have a warning message not blocking my app but annoying and very disturbing :
The 'value' attribute is invalid - The value
'System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' is invalid according
to its datatype 'connectionTypeTypes' - The Enumeration constraint
failed.
Where is the problem and the solution to get rid off this message ?
Many thanks in advance.

The log4net schema file (http://csharptest.net/downloads/schema/log4net.xsd) has this definition:
<xs:enumeration value="System.Data.SqlClient.SqlConnection, System.Data" />
If you change your setting to match this value, the warning will disappear.

Related

Why are my application settings retrieving different values to the ones I expect?

I have inherited an application which uses the configuration manager class to store and retrieve settings. In the app.config class there is custom section group "userSettings" which includes a "Server" property.
In the app.config file this value is defined as "a14". In Settings.Designer.vb the default is specified as "a5" yet when I try to access My.Settings.Server it brings back the value "a10", which is a value I previously used in the app.config file.
Not having much experience with the configuration manager, I am at a loss to determine where it is retrieving this value from and what I need to change so that it retrieves the correct server value.
For brevity, I have removed other settings from the code sample.
app.config:
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WorkstationApp.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
...
<userSettings>
<WorkstationApp.My.MySettings>
<setting name="Server" serializeAs="String">
<value>a14</value>
</setting>
</WorkstationApp.My.MySettings>
</userSettings>
Settings.Designer.vb:
<Global.System.Configuration.UserScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("a5")> _
Public Property Server() As String
Get
Return CType(Me("Server"),String)
End Get
Set
Me("Server") = value
End Set
End Property
Application code: (server is being set to "a10", but I want it to have the app.config value of "a14").
Dim Server As String = My.Settings.Server
It looks like I was expecting the wrong thing (well duh). I had these settings set as User settings, which are stored in the /appdata/ folder and had nothing to do with the app.config file at all.
The user config file had been set with the initial values and had never been modified subsequently with a My.Settings.Save. More details in this answer: Where are My.Settings saved in VB 2010 .NET?

Can't insert into SQL Server table with log4net AdoNetAppender

I am trying to understand how log4net works so I've added this to my app.config (I should add that Console Appender and FileAppender work perfectly, I only have trouble with the AdoNetAppender).
How can I debug this, to see if at least the connection to db succeeds?
The problem is the INSERT statement isn't executed.
I should add the
Data Source=MyWorkgroup\SQLEXPRESS;Initial Catalog=MyNewDatabase;User ID=juan;password=juan,
works perfectly when I try to connect to SQL Server manually, so not sure if that's the problem.
Also, the ConnectionType was taken from the official site:
https://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Appender.AdoNetAppender.ConnectionType.html
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.log4netConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="AdoNetAppender"
type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=MyWorkgroup\SQLEXPRESS;Initial Catalog=MyNewDatabase;User ID=juan;Password=juan;Pooling=False" />
<commandText value="INSERT INTO Logs([logDate],[logThread],[logMessage]) VALUES(getdate(),'1','1')" />
<commandType value="Text" />
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Later edit (after using the debug method recommended by David):
haacked.com is indeed extremely interesting, great tip! However, it seems that it's describing what I'm instructing log4net to log, but not the result of those actions(?), if I'm reading this well (no failed/or succeeded?)
e.g.
log4net: Setting Property [ConnectionType] to String value
[System.Data.SqlClien t.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToke n=b77a5c561934e089]
log4net: Setting Property [ConnectionString] to String value [Data
Source=MyWorkgroup\SQLEXPRESS; Initial
Catalog=MyNewDatabase;
User ID=juan; Password=juan;
Pooling=False] log4net: Setting Property [CommandText] to String value [INSERT INTO Logs([logDate],[logThread],[logMessage]) VALUES(getdate(),'1','1')] log4net:
Setting Property [CommandType] to CommandType value [Text] log4net:
Created Appender [AdoNetAppender] log4net: Adding appender named
[AdoNetAppender] to logger [root]. log4net: Hierarchy Threshold []
Piece of code that helped me obtain this info (in case the site should become unavailable) is:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\temp\log4netdiagn.txt" />
</listeners>
</trace>
</system.diagnostics>

Need config file for loading PRISM modules

I am trying to use PRISM for my application. I have been following Mike Taulty's video s on the same. I would like to know, if there is any sample code available for app.config file mentioned while loading modules without references. What I mean is I am looking to copy paste the configuration related information, instead of typing them from start to avoid mistakes. Also I would liek to know if the version number of the assembly is needed or not in the config file while we follow this approach.
My ModulesCatalog.xaml looks like this:
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<Modularity:ModuleInfoGroup InitializationMode="WhenAvailable">
<Modularity:ModuleInfo
Ref="MyCo.Module.SystemManager.SL.xap"
ModuleName="MembershipModule"
ModuleType="MyCo.Module.SystemManager.Module, MyCo.Module.SystemManager.SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</Modularity:ModuleInfoGroup>
<Modularity:ModuleInfoGroup InitializationMode="OnDemand">
<Modularity:ModuleInfo
Ref="MyCo.Module.Mobile.SL.xap"
ModuleName="MobileModule"
ModuleType="MyCo.Module.Mobile.Module, MyCo.Module.Mobile.SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Modularity:ModuleInfo
Ref="MyCo.Module.Dispatch.SL.xap"
ModuleName="DispatchModule"
ModuleType="MyCo.Module.Dispatch.Module, MyCo.Module.Dispatch.SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Modularity:ModuleInfo
Ref="MyCo.Module.Assets.SL.xap"
ModuleName="AssetsModule"
ModuleType="MyCo.Module.Assets.Module, MyCo.Module.Assets.SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</Modularity:ModuleInfoGroup>
</Modularity:ModuleCatalog>
I would also check PRISM samples (in PRISM installation folder)
Version needed, yes

System.DirectoryServices compilation problems

I'm using the code here : ASP.NET How to get List of Groups in Active Directory
Specifically :
using System.DirectoryServices;
DirectoryEntry objADAM = default(DirectoryEntry); //compiler error!
Problem: compiler says that it doesn't know what DirectoryEntry is.
I tried to add it to my web.config:
<assemblies>
<!-- ... -->
<add assembly="System.DirectoryServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
But it didn't seem to help so I tried to add a reference:
Someone suggested setting Copy Local = True but that didn't help either.
I noticed that I don't see a reference for System.DirectoryServices - only System.DirectoryServices.Accountmanagement
Can anyone help me get this code to compile? I'm not sure what to try next.
Take the .net 3.5 filter off in the Add Reference dialog, and then put a reference in your project to:
System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
You can take the filter off by changing you project setting : http://msdn.microsoft.com/en-us/library/bb398202.aspx
If you want to use the DirectoryEntry from the System.DirectoryServices namespace - you need to add a reference to the System.DirectoryServices assembly - NOT the System.DirectoryServices.AccountManagement assembly!

log4net.Config.XmlConfigurator.Configure() gives - "FileNotFoundException crossed a native/managed boundary"

This is only the 2nd time I've tried to use log4net. This is a Windows form app and I thought things are set up OK but I was not getting any logfile created. I found some advice about needing to explicitly make it read the app.config file so I added log4net.Config.XmlConfigurator.Configure() here:
public partial class GridForm : Form
{
public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));
public GridForm()
{
InitializeComponent();
// Initialize log4net (make him read the app.config file)
log4net.Config.XmlConfigurator.Configure(); // this gives the exception
}
I have a little uncertainty about how I'm referencing the log4net.dll.
I have 2 projects in the solution - CBMI.Common which is a class library project and CBMI.WinFormsUI which is my forms project (startup form).
I copied log4net.dll to the bin directory of CBMI.Common (NOT the bin\debug folder)
then added a reference in my forms project that points to that location: CBMI.Common\bin
This compiles without error. I think that it should be OK, but...
I cannot determine what to do from the exception raised and messages shown:
System.IO.FileNotFoundException crossed a native/managed boundary
Message=Could not load file or assembly 'Log4net' or one of its dependencies. The system cannot find the file specified.
Source=mscorlib
FileName=Log4net
FusionLog==== Pre-bind state information ===
LOG: User = HPpavilion\john
LOG: DisplayName = Log4net
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Log4net | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\john\documents\visual studio 2010\Projects\CBMI.LatitudePostConverter\CBMI.WinFormsUI\bin\Debug\CBMI.WinFormsUI.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net.DLL.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net/Log4net.DLL.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net.EXE.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net/Log4net.EXE.
StackTrace:
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.GetConfigType(String typeName, Boolean throwOnError)
at System.Configuration.Internal.DelegatingConfigHost.GetConfigType(String typeName, Boolean throwOnError)
at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
InnerException:
For completeness, I will add the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CBMI.WinFormsUI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<userSettings>
<CBMI.WinFormsUI.Properties.Settings>
<setting name="DefaultRootFolder" serializeAs="String">
<value />
</setting>
</CBMI.WinFormsUI.Properties.Settings>
</userSettings>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log-file.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
The fix for me was to insert [assembly: log4net.Config.XmlConfigurator(Watch = true)] into assemblyinfo.cs

Resources