FileAppender does not work with File property? - log4net

Using log4net 1.2.11 on .net framework 3.5, this works:
var fileAppender = new log4net.Appender.FileAppender(layout, "check.log", true);
log4net.Config.BasicConfigurator.Configure(fileAppender);
log4net.LogManager.GetLogger(typeof(Program)).Info("constructor");
Except that it gives the warning:
Warning 1 'log4net.Appender.FileAppender.FileAppender(log4net.Layout.ILayout,
string, bool)' is obsolete: 'Instead use the default constructor and
set the Layout, File & AppendToFile properties
But if I use the properties it does not work
var fileAppender = new log4net.Appender.FileAppender()
{ Layout = layout, File = "check.log", AppendToFile = true };
log4net.Config.BasicConfigurator.Configure(fileAppender);
log4net.LogManager.GetLogger(typeof(Program)).Info("prop1");
That is, nothing gets written to the file. I have also tried a full path ("c:\check.log") and assigning to properties after creating the appender with default constructor.
What am I doing wrong?

I did not try it but looking at the log4net source code I can only assume that you need to call ActivateOptions on the file appender in order to make things work.

Related

How can I find the path of a file in kotlin?

From what I know you can find the path of a file in the internal storage with java simply by using:
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path + "/filename");
But how do I do this using kotlin?
In Kotlin, when you see a getter method from a Java class, you can omit the get, change the first letter to lower-case, and use it like a Kotlin property:
val path = context.filesDir.absolutePath
val file = File("$path/filename")

How to set `invalidAttributeNamePrefix` value in Java?

Suppose I'm cleaning some html using HtmlCleaner (v2.18) and I want to set the property invalidAttributeNamePrefix (see section Cleaner parameters) to some value, i.e.: data-.
This way an attribute my-custom-attr="my-value" in the HTML will be transformed to data-my-custom-attr="my-value".
How can I do that? I wasn't able to find any example for the Java usage.
You can take as reference this piece of code:
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
properties.setOmitComments(true);
// properties.setInvalidAttributeNamePrefix("data-"); there is no such method
// html is a declared variable which contains some html content
TagNode rootTagNode = cleaner.clean(html);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(properties);
String cleanedHtml = xmlSerializer.getAsString(rootTagNode);
Upgrading to version 2.22 solves this.
Now it can be done
// ...
properties.setInvalidXmlAttributeNamePrefix("data-");
//...

How to programmatically update .rgs files to reflect changes made in IDL files?

Is there any tool to update .rgs files to reflect change made in the IDL ?
rgs files are created by the ATL control wizzard but I can't find a way to refresh thoses files.
When we change the uuid of an interface (within the .IDL file), we are forced to changed by hand the "hard copy" values in those .rgs files. This is quiet prone to error.
I found this interesting project that intend to fill this gap but, accordingly the last comments, it didn't works any more since VC2005.
ATL CAtlModule implementation offers virtual CAtlModule::AddCommonRGSReplacements which you can override and add substitutions to remove hardcoded RGS values.
For example, my typical ATL code looks like this:
class CFooModule :
public CAtlDllModuleT<CFooModule>
{
[...]
// CAtlModule
HRESULT AddCommonRGSReplacements(IRegistrarBase* pRegistrar)
{
// Error handling omitted for code brevity
__super::AddCommonRGSReplacements(pRegistrar);
ATLASSERT(m_libid != GUID_NULL);
pRegistrar->AddReplacement(L"LIBID", _PersistHelper::StringFromIdentifier(m_libid));
pRegistrar->AddReplacement(L"FILENAME", CStringW(PathFindFileName(GetModulePath())));
pRegistrar->AddReplacement(L"DESCRIPTION", CStringW(AtlLoadString(IDS_PROJNAME)));
return S_OK;
}
In COM classes I override UpdateRegistry method to add tokens with third parameter of standard call _pAtlModule->UpdateRegistryFromResource.
As a result, many .RGS are shared between COM classes because hardcoded values are replaced with tokens. Specifically, there are no GUIDs in RGS files, e.g.:
HKCR
{
NoRemove CLSID
{
ForceRemove %CLSID% = s '%DESCRIPTION%'
{
InprocServer32 = s '%MODULE%'
{
val ThreadingModel = s 'Both'
}
val AppID = s '%APPID%'
TypeLib = s '%LIBID%'
}
}
}
I'm not able to understand how %CLSID% is replaced with the COM class CLSID in roman-r's answer. There seem to be something missing in the answer.
Alternative solution from CodeProject: Registry Map for RGS files.
This solution introduces a custom registrymap.hpp header with a DECLARE_REGISTRY_RESOURCEID_EX extension that allows you to add RGS substitution macros to your COM classes. Example:
BEGIN_REGISTRY_MAP(CClassName)
REGMAP_ENTRY("PROGID", "MyLibrary.ClassName")
REGMAP_ENTRY("VERSION", "1")
REGMAP_ENTRY("DESCRIPTION", "ClassName Class")
REGMAP_UUID ("CLSID", CLSID_ClassName)
REGMAP_UUID ("LIBID", LIBID_MyLibraryLib)
REGMAP_ENTRY("THREADING", "Apartment")
END_REGISTRY_MAP()

SOAPUI Load Custom Properties from file using groovy

I am trying to write a groovy script which loads the custom properties for a test suite using information from a properties file.
The properties file has around 6 different attributes
I have had a look at quite a few different methods i.e Loading from Properties test step and trying to expand the properties with groovy, but have not been successful.
If anyone could advise on how to achieve this, it would be much appreciated.
Thanks in advance.
Here is the groovy script which reads a property file and set them at test suite level:
def props = new Properties()
//replace the path with your file name below. use / instead of \ as path separator even on windows platform.
new File("/absolute/path/of/test.properties").withInputStream { s ->
props.load(s)
}
props.each {
context.testCase.testSuite.setPropertyValue(it.key, it.value)
}
The above script load test suite level for the current suite where the groovy script is present.
Unfortunately, in my case I want to have the properties in the same order as the input file, ie. sorted, and this methode does not work.
I wanted to load a 'Project properties' file containing sorted properties and each time I used this method it stored them unsorted.
I had to use a more straightforward method (see below). If anyone knows about a more elegant/practical way to do it, I'm interested
def filename = context.expand( '${#TestCase#filename}' )
def propertiesFile = new File(filename)
assert propertiesFile.exists(), "$filename does not exist"
project = testRunner.testCase.testSuite.project
//Remove properties
project.propertyNames.collect{project.removeProperty(it)}
//load the properties of external file
propertiesFile.eachLine {
line->
firstIndexOf = line.indexOf('=') // properties as set as key=value in the file
key = line.substring(0, firstIndexOf)
value = line.substring(firstIndexOf+1)
project.setPropertyValue(key, value)
}

Create an Empty FileInfo Object in C# without an existing file

I added an alternative code-path using an input string rather than reading from a file.
I would require an empty FileInfo object as I have many instances which access the Name, Length and Extension property.
Ideally I am looking for something like
FileInfo _fileinfo = new FileInfo(File.Empty);
However there is only one FileInfo constructor, which appears to require a valid file. Any solution to creating an empty-initialized FileInfo object, which does not require the creation of an empty dummy file?
I just came across a similar problem. What do you think about starting with:
FileInfo _fileinfo = null;
After that, you could just do:
_fileinfo = new FileInfo(<string of file with path>);
You would then have an object that you could pass as parameter to your methods. Don't foget to check if your object is null before you try to get the values for .Name and so on
if(null != _fileinfo)
{
//some code
}
As pointed out in the comments, FileInfo will happily construct with non-existant paths, but they have to be valid path strings (which you call a 'valid file'). (See https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=net-5.0 for what is considered a valid path.)
The solution would be to simply write
FileInfo _fileinfo = new FileInfo(#"C:\NonExistant.txt");
instead of
FileInfo _fileinfo = new FileInfo(File.Empty);
You can then think of your own test to see if you're dealing with the 'Empty' FileInfo object, such as checking drive letter, extension, File.Exists tests and such.

Resources